Android Dialog详解及实例代码
程序员文章站
2023-11-24 18:31:28
android dialog详解及实例代码
概述:
android开发中最常用的就是dialog类,除了自定义dialog布局,最多的就是用在弹出对话框、进...
android dialog详解及实例代码
概述:
android开发中最常用的就是dialog类,除了自定义dialog布局,最多的就是用在弹出对话框、进度条、输入框、单选、复选框。
1、选择对话框:
alertdialog.builder dialog = new alertdialog.builder(this); dialog.settitle("选择对话框"); dialog.setmessage("请选择确认或取消"); dialog.setcancelable(false); //设置按下返回键不能消失 dialog.setpositivebutton("确认", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(mainactivity.this, "您选择了确认", toast.length_long).show(); } }); dialog.setnegativebutton("取消", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(mainactivity.this, "您选择了取消", toast.length_long).show(); } }); dialog.show();//显示弹出窗口
2、进度条
progressdialog progressdialog = new progressdialog(this); progressdialog.settitle("显示进度条"); progressdialog.setmessage("加载中..."); progressdialog.setcancelable(true); progressdialog.show();
3、弹出输入框
alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("弹出输入框"); builder.setmessage("输入内容"); final edittext edittext = new edittext(this); builder.setview(edittext); builder.setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(mainactivity.this, "您输入了" + edittext.gettext().tostring(), toast.length_short).show(); } }); builder.setnegativebutton("取消", null); builder.show();
4、弹出单选框
final alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("弹出单选框"); final string[] choice = new string[]{"java", "c++", "php"}; builder.setsinglechoiceitems(choice, 0, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(mainactivity.this, "您选择了" + choice[which], toast.length_long).show(); dialog.dismiss(); // 让窗口消失 } }); builder.create().show();
5、弹出复选框
final alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("弹出多选框"); final string[] choice = new string[]{"android", "ios", "windos phone"}; // 选择项 final boolean[] ischeck = new boolean[choice.length]; // 是否被选中 builder.setmultichoiceitems(choice, null, new dialoginterface.onmultichoiceclicklistener() { @override public void onclick(dialoginterface dialog, int which, boolean ischecked) { if (ischecked == true) ischeck[which] = true; else if(ischecked == false) ischeck[which] = false; } }); builder.setnegativebutton("取消", null); builder.setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { string isselected = "您选择了"; for (int i = 0; i < choice.length; i++) { if (ischeck[i] == true) { isselected += choice[i]+","; // 将选择的内容加上 } } toast.maketext(mainactivity.this, isselected, toast.length_long).show(); } }); builder.create(); builder.show();
6、自定义布局的dialog
alertdialog.builder builder = new alertdialog.builder(this); final alertdialog dialog = builder.create(); view view = view.inflate(this, r.layout.personal_modifyhead_dialog, null); dialog.setview(view, 0, 0, 0, 0);// 设置边距为0,保证在2.x的版本上运行没问题 dialog.show(); autorelativelayout rl_photograph = (autorelativelayout) view.findviewbyid(r.id.rl_photograph); rl_photograph.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { presenter.modiftheadfromphotograph(); dialog.dismiss(); } }); autorelativelayout rl_album = (autorelativelayout) view.findviewbyid(r.id.rl_album); rl_album.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { presenter.modifyheadfromalbum(); dialog.dismiss(); } });
上面就是日常开发中常用的一些,在此记录,以便日后查询。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
推荐阅读
-
Android中SharedPreference详解及简单实例
-
Android 中 Tweened animation的实例详解
-
Android 中 ActivityLifecycleCallbacks的实例详解
-
Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout实例详解
-
Java并发编程之Semaphore(信号量)详解及实例
-
python 排序算法总结及实例详解
-
IOS NSUserDefault 记住用户名及密码功能的实例代码
-
Android ListView 滚动条的设置详解及实例代码
-
Android编程中activity的完整生命周期实例详解
-
Android编程之基于Log演示一个activity生命周期实例详解