Android中Dialog对话框的使用小结
程序员文章站
2023-11-13 13:03:34
前言
最近有些空时间,所以花了一个小时对dialog对话框使用小结一下,比较基础,希望对你学习有帮助,大牛请直接关闭网页。如果你是新手,建议你亲自敲一遍代码。
先看一下...
前言
最近有些空时间,所以花了一个小时对dialog对话框使用小结一下,比较基础,希望对你学习有帮助,大牛请直接关闭网页。如果你是新手,建议你亲自敲一遍代码。
先看一下效果:
dialog对话框使用小结
一、普通对话框
alertdialog.builder builder = new alertdialog.builder(activity); builder.settitle("温馨提示");//标题 builder.setmessage("天气冷,注意保暖"); builder.seticon(r.mipmap.ic_launcher); builder.create(); builder.show();
普通对话框
二、确定取消对话框
builder.settitle("确定取消对话框"); builder.setmessage("请选择确定或取消"); builder.seticon(r.mipmap.ic_launcher); builder.setpositivebutton("确定", new dialoginterface.onclicklistener() { //正能量按钮 positive @override public void onclick(dialoginterface dialog, int which) { toast.maketext(activity, "你点击了确定", toast.length_short).show(); } }); builder.setnegativebutton("取消", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(activity, "你选择了取消", toast.length_short).show(); } }); builder.create().show();
确定取消对话框
三、多按钮对话框
builder.settitle("多个按钮对话框"); builder.setmessage("请选择"); builder.seticon(r.mipmap.ic_launcher); builder.setpositivebutton("我没玩够", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(activity, "继续浏览精彩内容", toast.length_short).show(); } }); builder.setneutralbutton("开启", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(activity, "起床了", toast.length_short).show(); } }); builder.setnegativebutton("我累了,要休息一下", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(activity, "欢迎再来", toast.length_short).show(); } }); builder.create().show();
多按钮对话框
四、列表对话框
final string arritem[] = getresources().getstringarray(r.array.aikaifa); builder.setitems(arritem, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(activity, "你选择了第" + arritem[which], toast.length_short).show(); } }); builder.create().show();
列表对话框
五、带adapter的对话框
builder.settitle("带adapter的对话框"); builder.seticon(r.mipmap.ic_launcher); final list<map<string, object>> list = new arraylist<map<string, object>>(); int arrimg[] = {r.mipmap.ic_launcher, r.mipmap.ic_launcher, r.mipmap.ic_launcher, r.mipmap.ic_launcher, r.mipmap.ic_launcher, r.mipmap.ic_launcher, r.mipmap.ic_launcher, r.mipmap.ic_launcher, r.mipmap.ic_launcher, r.mipmap.ic_launcher, r.mipmap.ic_launcher, r.mipmap.ic_launcher}; for (int i = 0; i < arrimg.length; i++) { map<string, object> map = new hashmap<string, object>(); map.put("img", arrimg[i]); map.put("title", "爱开发" + i); list.add(map); } simpleadapter adapter = new simpleadapter(activity, list, r.layout.list_item, new string[]{"img", "title"}, new int[]{r.id.iv, r.id.tv}); builder.setadapter(adapter, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(activity, "你选择了" + list.get(which).get("title").tostring().trim(), toast.length_short).show(); } }); builder.create().show();
带adapter的对话框
六、单选对话框
builder.settitle("单选对话框"); builder.seticon(r.mipmap.ic_launcher); builder.setsinglechoiceitems(r.array.aikaifa, 0, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(activity, which+"", toast.length_short).show(); } }); builder.setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { } }); builder.create().show();
单选对话框
七、多选对话框
builder.settitle("多选对话框"); builder.seticon(r.mipmap.ic_launcher); builder.setmultichoiceitems(r.array.aikaifa, null, new dialoginterface.onmultichoiceclicklistener() { @override public void onclick(dialoginterface dialog, int which, boolean ischecked) { toast.maketext(activity, which+""+ischecked, toast.length_short).show(); } }); builder.create().show();
多选对话框
八、日期对话框
datepickerdialog datepickerdialog=new datepickerdialog(activity, new datepickerdialog.ondatesetlistener() { @override public void ondateset(datepicker view, int year, int monthofyear, int dayofmonth) { toast.maketext(activity, year+"年"+(monthofyear+1)+"月"+dayofmonth+"日", toast.length_short).show(); } }, 2017, 02, 9); datepickerdialog.show();
日期对话框
九、时间对话框
timepickerdialog timepickerdialog=new timepickerdialog(activity, new timepickerdialog.ontimesetlistener() { @override public void ontimeset(timepicker view, int hourofday, int minute) { toast.maketext(activity, hourofday+"小时"+minute+"分钟", toast.length_short).show(); } }, 17, 49, true); timepickerdialog.show();
时间对话框
十、自定义对话框
view view= layoutinflater.from(activity).inflate(r.layout.dialog_login, null); builder.setview(view); builder.create(); final edittext et_phone=(edittext)view.findviewbyid(r.id.et_phone); final edittext et_password=(edittext)view.findviewbyid(r.id.et_password); button btn_submit=(button)view.findviewbyid(r.id.btn_submit); btn_submit.setonclicklistener( new view.onclicklistener() { @override public void onclick(view view) { toast.maketext(activity, "手机号码:"+et_phone.gettext().tostring()+" 短信验证码:"+et_password.gettext().tostring(), toast.length_short).show(); } }); builder.show();
自定义对话框
项目设计到的xml
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f5f5f5" android:orientation="horizontal" android:padding="10dp"> <imageview android:id="@+id/iv" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/tv" android:text="标题" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </linearlayout>
dialog_login.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical"> <linearlayout android:id="@+id/linearlayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerinparent="true" android:layout_margin="8dp" android:orientation="vertical" android:padding="5dp"> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginbottom="10dp" android:layout_margintop="10dp" android:gravity="center_horizontal" android:text="验证手机号码" android:textcolor="#414141" /> <edittext android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" android:hint="请输入手机号码" android:inputtype="number" android:maxlength="11" android:paddingleft="10dp" android:textsize="14sp" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginbottom="20dp" android:layout_margintop="10dp"> <edittext android:id="@+id/et_password" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="4" android:gravity="center_vertical" android:hint="请输入短信验证码" android:inputtype="number" android:maxlength="6" android:paddingleft="10dp" android:textsize="14sp" /> <textview android:id="@+id/tv_get_code" android:layout_width="0dp" android:layout_height="48dp" android:layout_marginleft="10dp" android:layout_weight="2" android:enabled="false" android:gravity="center" android:text="点击获取" android:textcolor="#000000" /> </linearlayout> <button android:id="@+id/btn_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:textcolor="#000000" android:gravity="center" android:paddingbottom="10dp" android:paddingtop="10dp" android:text="提交" /> </linearlayout> </relativelayout>
总结
以上就是这篇文章的全部内容了,希望本文的内容对各位android开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
推荐阅读