Android中AlertDialog各种对话框的用法实例详解
程序员文章站
2024-02-29 13:37:10
目标效果:
程序运行,显示图一的几个按钮,点击按钮分别显示图二到图六的对话框,点击对话框的某一项或者按钮,也会显示相应的吐司输出。
1.ac...
目标效果:
程序运行,显示图一的几个按钮,点击按钮分别显示图二到图六的对话框,点击对话框的某一项或者按钮,也会显示相应的吐司输出。
1.activity_main.xml页面存放五个按钮。
activity_main.xml页面:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <button android:id="@+id/btnsure" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="10dp" android:text="确认对话框"/> <button android:id="@+id/btnradio" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="60dp" android:text="单选对话框"/> <button android:id="@+id/btncheck" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="110dp" android:text="多选对话框"/> <button android:id="@+id/btnlist" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="160dp" android:text="列表对话框"/> <button android:id="@+id/btnmy" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="210dp" android:text="自定义对话框"/> </relativelayout>
2.新建dialog.xml页面,作为最后一个自定义对话框的布局页面。
dialog.xml页面:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <edittext android:id="@+id/edinput" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" > <requestfocus /> </edittext> <button android:id="@+id/btnok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定" /> </linearlayout> <imageview android:id="@+id/ivpicture" android:layout_width="wrap_content" android:layout_height="280dp" android:src="@drawable/white" /> <textview android:id="@+id/textview1" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview" /> </linearlayout>
3.mainactivity.java页面处理对话框的弹出及点击事件。
mainactivity.java页面:
package com.example.alertdialog; import android.os.bundle; import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.toast; public class mainactivity extends activity implements onclicklistener { private button btnsure,btnradio,btncheck,btnlist,btnmy; private string[] sexlist={"男","女"};//单选列表 private string[] likelist={"篮球","足球","打游戏","听音乐","看电影"};//多选列表 private string[] itemlist={"项目经理","策划","测试","美工","程序员"};//列表 @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); getid();//获取控件id click();//按钮绑定点击事件 } /*获取控件id*/ private void getid() { btnsure = (button) findviewbyid(r.id.btnsure); btnradio=(button) findviewbyid(r.id.btnradio); btncheck=(button) findviewbyid(r.id.btncheck); btnlist=(button) findviewbyid(r.id.btnlist); btnmy=(button) findviewbyid(r.id.btnmy); } /*按钮绑定点击事件*/ private void click() { btnsure.setonclicklistener(this); btnradio.setonclicklistener(this); btncheck.setonclicklistener(this); btnlist.setonclicklistener(this); btnmy.setonclicklistener(this); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override public void onclick(view view) { switch (view.getid()) { case r.id.btnsure: showdialog1();//确认对话框 break; case r.id.btnradio: showdialog2();//单选对话框 break; case r.id.btncheck: showdialog3();//多选对话框 break; case r.id.btnlist: showdialog4(); break; case r.id.btnmy: showdialog5(); break; } } /*确认对话框*/ private void showdialog1() { alertdialog.builder builder=new alertdialog.builder(this); builder.settitle("确认对话框");//设置标题 builder.seticon(r.drawable.ic_launcher);//设置图标 builder.setmessage("确认对话框提示内容");//设置内容 /*添加对话框中确定按钮和点击事件*/ builder.setpositivebutton("确定",new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface arg0, int arg1) { toast.maketext(mainactivity.this,"点击了确定按钮",toast.length_short).show(); } }); /*添加对话框中取消按钮和点击事件*/ builder.setnegativebutton("取消",new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface arg0, int arg1) { toast.maketext(mainactivity.this,"点击了取消按钮",toast.length_short).show(); } }); alertdialog dialog=builder.create();//获取dialog dialog.show();//显示对话框 } /*单选对话框*/ private void showdialog2() { alertdialog.builder builder=new alertdialog.builder(this); builder.settitle("性别");//设置标题 builder.seticon(r.drawable.ic_launcher);//设置图标 /*参数一位单选列表文字,参数二为默认第几个选中(-1默认不选中),参数三是创建监听器*/ builder.setsinglechoiceitems(sexlist,-1,new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { string sex=sexlist[which]; toast.maketext(mainactivity.this,"这个人性别为"+sex, toast.length_short).show(); } }); /*添加对话框中取消按钮点击事件*/ builder.setnegativebutton("取消",new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss();//关闭对话框 } }); alertdialog dialog=builder.create();//获取dialog dialog.show();//显示对话框 } /*多选对话框*/ private void showdialog3() { alertdialog.builder builder=new alertdialog.builder(this); builder.settitle("爱好");//设置标题 builder.seticon(r.drawable.ic_launcher);//设置图标 /*参数同单选对话框一样,另外第二个参数默认不选中为null,而不是-1*/ builder.setmultichoiceitems(likelist,null,new dialoginterface.onmultichoiceclicklistener() { @override public void onclick(dialoginterface dialog, int which, boolean ischecked) { if(ischecked){ toast.maketext(mainactivity.this,"我喜欢"+likelist[which],toast.length_short).show(); }else{ toast.maketext(mainactivity.this,"我不喜欢"+likelist[which],toast.length_short).show(); } } }); /*添加对话框中取消按钮点击事件*/ builder.setnegativebutton("取消",new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss();//关闭对话框 } }); alertdialog dialog=builder.create();//获取dialog dialog.show();//显示对话框 } /*列表对话框*/ private void showdialog4() { alertdialog.builder builder=new alertdialog.builder(this); builder.settitle("部门列表");//设置标题 builder.seticon(r.drawable.ic_launcher);//设置图标 builder.setitems(itemlist,new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(mainactivity.this,"我点击了"+itemlist[which],toast.length_short).show(); } }); alertdialog dialog=builder.create();//获取dialog dialog.show();//显示对话框 } /*自定义对话框*/ private void showdialog5() { layoutinflater inflater=layoutinflater.from(this); view view=inflater.inflate(r.layout.dialog,null);//获取自定义布局 alertdialog.builder builder=new alertdialog.builder(this); builder.settitle("自定义对话框");//设置标题 builder.seticon(r.drawable.ic_launcher);//设置图标 builder.setview(view);//设置自定义样式布局到对话框 alertdialog dialog=builder.create();//获取dialog dialog.show();//显示对话框 } }
4.运行就出现目标效果了。
关于android中alertdialog各种对话框的用法就给大家介绍这么多,希望对大家有所帮助!