一.普通的AlertDialog: 1.AlertDialog的结构:
2.如何创建一个AlertDialog对象:
步骤:
3.代码实现:
布局只有一个按钮,比较简单,这里就略过了
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 public class MainActivity extends Activity { private Button btnshow; private AlertDialog.Builder build = null; private AlertDialog alert = null; @Override protected void onCreate(Bundle savedInstanceState ) { super.onCreate(savedInstanceState ) ; setContentView(R.layout .activity_main ) ; btnshow = (Button) findViewById(R.id .btnshow ) ; btnshow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v ) { alert = new AlertDialog.Builder(MainActivity.this ) .create() ; alert.setIcon(R.drawable .kabi ) ; alert.setTitle("系统提示:" ) ; alert.setMessage("这是一个普通的AlertDialog,\n依次有取消,中立,确定按钮" ) ; alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog , int which ) { Toast . makeText(getApplicationContext () , "你点击了确定按钮" , Toast.LENGTH_SHORT).show() ; } }); alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog , int which ) { Toast . makeText(getApplicationContext () , "你点击了取消按钮" , Toast.LENGTH_SHORT).show() ; } }); alert.setButton(DialogInterface.BUTTON_NEUTRAL, "中立" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog , int which ) { Toast . makeText(getApplicationContext () , "你点击了中立按钮" , Toast.LENGTH_SHORT).show() ; } }); alert.show() ; } }); } }
运行结果:
代码解释:
1.这里的话我们是直接通过新建一个AlertDialog.Builder对象,同时调用create()方法创建了一个AlertDialog对象
2.这里要注意哦,普通按钮的监听器对象为View.OnClickListener,而AlertDialog的按钮监听器对象为DialogInterface.OnClickListener
3.按钮的话最多只能有三个,可以一个或者两个,确定,中立,取消按钮,setButton();第一个参数是决定了是哪种按钮:都是DialogInterface.****
BUTTON_POSITIVE:确定按钮 BUTTON_NEGATIVE:取消按钮 BUTTON_NEUTRAL:中立按钮
4.这里还要注意一点很容易出错的,就是
很容易出现以下错误:
android.view.WindowManager$BadTokenException: Unable to add window – token null is not for an application 或:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
这里的话是因为AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
里面的这个MainActivity必须写成当前的Activity.this,不能够用getApplicationContext(),或者直接this,都会报错的哦!
源码链接:http://pan.baidu.com/s/1BZPo2
二.设置一个带列表,单选列表,多选列表的对话框 这里为了方便,就把三种对话框的实现放到一起了,这里通过三个按钮弹出对应的窗体。
代码:
main.xml布局文件就只有三个按钮,并为其设置id
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 public class MainActivity extends Activity { private Button btnlist; private Button btnradio; private Button btncheck; private AlertDialog alert = null ; private AlertDialog.Builder builder = null ; private boolean [] checkItems; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnlist = (Button) findViewById(R.id.btnlist); btnradio = (Button) findViewById(R.id.btnradio); btncheck = (Button) findViewById(R.id.btncheck); btnlist.setOnClickListener(new View .OnClickListener() { @Override public void onClick (View v) { final String[] lesson = new String []{"语文" ,"数学" ,"英语" ,"化学" ,"生物" ,"物理" ,"体育" }; builder = new AlertDialog .Builder(MainActivity.this ); builder.setIcon(R.drawable.yibu); builder.setTitle("请选择你喜欢的课程" ); builder.setItems(lesson, new DialogInterface .OnClickListener() { @Override public void onClick (DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你选择了" +lesson[which],Toast.LENGTH_SHORT).show(); } }); alert = builder.create(); alert.show(); } }); btnradio.setOnClickListener(new View .OnClickListener() { @Override public void onClick (View v) { final String[] fruits = new String []{"苹果" ,"雪梨" ,"香蕉" ,"葡萄" ,"西瓜" }; builder = new AlertDialog .Builder(MainActivity.this ); builder.setIcon(R.drawable.leiyibu); builder.setTitle("请选择你喜欢的水果,只能选一个哦!" ); builder.setSingleChoiceItems(fruits, 0 , new DialogInterface .OnClickListener() { @Override public void onClick (DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你选择了" +fruits[which], Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定" , null ); alert = builder.create(); alert.show(); } }); btncheck.setOnClickListener(new View .OnClickListener() { @Override public void onClick (View v) { final String[] menu = new String []{"水煮豆腐" ,"萝卜牛腩" ,"葱爆瘦肉" }; checkItems = new boolean []{false ,false ,false }; builder = new AlertDialog .Builder(MainActivity.this ); builder.setIcon(R.drawable.huoyibu); builder.setTitle("点选你喜欢的菜肴:" ); builder.setMultiChoiceItems(menu, checkItems, new OnMultiChoiceClickListener () { @Override public void onClick (DialogInterface dialog, int which, boolean isChecked) { checkItems[which] = isChecked; } }); builder.setPositiveButton("确定" , new DialogInterface .OnClickListener() { @Override public void onClick (DialogInterface dialog, int which) { String result = "" ; for (int i = 0 ;i < checkItems.length;i++) { if (checkItems[i]) result += menu[i]+" " ; } Toast.makeText(getApplicationContext(),"客官你点了:" +result, Toast.LENGTH_SHORT).show(); } }); builder.create().show(); } }); } }
运行截图:
代码解释:
1.这几个的区别主要是设置内容那里,普通的是setItem();单选按钮时setSingleChoiceItems;复选框是setMultiChoiceItems();
2.这里的setOnClickListener 是DialogInterface.OnClickListener,而非View的那个
3.第三个复选框我们是通过boolean数组判断是否选中的,确定按钮中的触发事件是:OnMultiChoiceClickListener,参数依次为显示的数组,boolean的状态数组
4.boolean写成Boolean是会报错的哦!!!!
源码下载:http://pan.baidu.com/s/1y2FlK
三.自定义一个AlertDialog: main.xml中只定义一个简单的按钮
首先,自定义一个我们想要的布局,我们这里做的是一个登陆的布局
login.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <?xml version="1.0" encoding="utf-8" ?> <TableLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width ="match_parent" android:layout_height ="match_parent" android:orientation ="vertical" > <TableRow > <TextView android:layout_width ="match_parent" android:layout_height ="wrap_content" android:text ="账号:" android:textSize ="15sp" /> <EditText android:id ="@+id/txtname" android:layout_width ="match_parent" android:layout_height ="wrap_content" android:hint ="请输入您的账号: " android:selectAllOnFocus ="true" /> </TableRow > <TableRow > <TextView android:layout_width ="match_parent" android:layout_height ="wrap_content" android:text ="密码:" android:textSize ="12sp" /> <EditText android:id ="@+id/txtpassword" android:layout_width ="match_parent" android:layout_height ="wrap_content" android:hint ="请输入您的密码: " android:password ="true" /> </TableRow > </TableLayout >
解释:这些属性在前面的组件都有的,不会的可以查看前面的博文
接着,就是MainActivity.java,其实自定义和普通的区别就是用了setView方法,我们只需要为login布局创建一个view即可
这里的话用到inflate动态加载布局
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class MainActivity extends Activity { private Button btnshow; private AlertDialog.Builder build = null; private EditText edit; @Override protected void onCreate(Bundle savedInstanceState ) { super.onCreate(savedInstanceState ) ; setContentView(R.layout .activity_main ) ; btnshow = (Button) findViewById(R.id .btnshow ) ; btnshow.setOnClickListener(new OnClickListener() { @Override public void onClick(View v ) { TableLayout table = (TableLayout) getLayoutInflater() .inflate(R . layout.login, null); edit = (EditText) table.findViewById(R.id .txtname ) ; build = new AlertDialog.Builder(MainActivity.this ) ; build.setIcon(R.drawable .pipi ) ; build.setTitle("登陆界面:" ) ; build.setView(table ) ; build.setPositiveButton("登陆" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog , int which ) { Toast . makeText(getApplicationContext () , "欢迎你" +edit.getText() , Toast.LENGTH_SHORT).show() ; } }); build.setNegativeButton("取消" , new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog , int which ) { Toast . makeText(getApplicationContext () , "感谢您的使用!" , Toast.LENGTH_SHORT).show() ; } }); build.create() .show() ; } }); } }
运行截图:
代码解释:
1.这里的话要注意动态加载布局那里:TableLayout table = (TableLayout) getLayoutInflater().inflate(R.layout.login, null);
edit = (EditText) table.findViewById(R.id.txtname);
因为我们想获取的是login的布局中的EditText的文本内容,这里findViewByid的话要在前面加上这个动态布局的table.FindViewById.不然会报空指针异常
2.再次说明一下,我们自定义布局的核心是Builder类的setView()方法,只要实例化一个View放进去即可
程序源码:http://pan.baidu.com/s/1ziCsP
转自:http://blog.csdn.net/coder_pig/article/details/16828579