今天我用自己写的一个Demo 和大家详细介绍一个Android中的对话框的使用技巧。
1.确定取消对话框
对话框中有2个按钮 通过调用 setPositiveButton 方法 和 setNegativeButton 方法 可以设置按钮的显示内容以及按钮的监听事件。
我们使用AlerDialog 创建对话框
1 | AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog. this ); |
使用builder设置对话框的title button icon 等等
01 | builder.setIcon(R.drawable.icon); |
02 | builder.setTitle( "你确定要离开吗?" ); |
03 | builder.setPositiveButton( "确定" , new DialogInterface.OnClickListener() { |
04 | public void onClick(DialogInterface dialog, int whichButton) { |
09 | builder.setNegativeButton( "取消" , new DialogInterface.OnClickListener() { |
10 | public void onClick(DialogInterface dialog, int whichButton) { |
15 | builder.create().show(); |
这个dialog用于现实onClick后监听的内容信息
1 | private void showDialog(String str) { |
2 | new AlertDialog.Builder(MainDialog. this ) |
2.多个按钮信息框
01 | AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog. this ); |
02 | builder.setIcon(R.drawable.icon); |
03 | builder.setTitle( "投票" ); |
04 | builder.setMessage( "您认为什么样的内容能吸引您?" ); |
05 | builder.setPositiveButton( "有趣味的" , new DialogInterface.OnClickListener() { |
06 | public void onClick(DialogInterface dialog, int whichButton) { |
07 | showDialog( "你选择了有趣味的" ); |
10 | builder.setNeutralButton( "有思想的" , new DialogInterface.OnClickListener() { |
11 | public void onClick(DialogInterface dialog, int whichButton) { |
12 | showDialog( "你选择了有思想的" ); |
15 | builder.setNegativeButton( "主题强的" , new DialogInterface.OnClickListener() { |
16 | public void onClick(DialogInterface dialog, int whichButton) { |
17 | showDialog( "你选择了主题强的" ); |
20 | builder.create().show(); |
3.列表框
这个数组用于列表选择
01 | final String[] mItems = { "item0" , "item1" , "itme2" , "item3" , "itme4" , "item5" , "item6" }; |
04 | AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog. this ); |
05 | builder.setTitle( "列表选择框" ); |
06 | builder.setItems(mItems, new DialogInterface.OnClickListener() { |
07 | public void onClick(DialogInterface dialog, int which) { |
09 | showDialog( "你选择的id为" + which + " , " + mItems[which]); |
12 | builder.create().show(); |
4.单项选择列表框
mSingleChoice 用于记录单选中的ID
01 | int mSingleChoiceID = - 1 ; |
03 | AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog. this ); |
06 | builder.setIcon(R.drawable.icon); |
07 | builder.setTitle( "单项选择" ); |
08 | builder.setSingleChoiceItems(mItems, 0 , new DialogInterface.OnClickListener() { |
09 | public void onClick(DialogInterface dialog, int whichButton) { |
10 | mSingleChoiceID = whichButton; |
11 | showDialog( "你选择的id为" + whichButton + " , " + mItems[whichButton]); |
14 | builder.setPositiveButton( "确定" , new DialogInterface.OnClickListener() { |
15 | public void onClick(DialogInterface dialog, int whichButton) { |
16 | if (mSingleChoiceID > 0 ) { |
17 | showDialog( "你选择的是" + mSingleChoiceID); |
21 | builder.setNegativeButton( "取消" , new DialogInterface.OnClickListener() { |
22 | public void onClick(DialogInterface dialog, int whichButton) { |
26 | builder.create().show(); |
5.进度条框
点击进度条框按钮后 开启一个线程计算读取的进度 假设读取结束为 100
Progress在小于100的时候一直在线程中做循环++ 只到读取结束后,停止线程。
01 | mProgressDialog = new ProgressDialog(MainDialog. this ); |
02 | mProgressDialog.setIcon(R.drawable.icon); |
03 | mProgressDialog.setTitle( "进度条窗口" ); |
04 | mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
05 | mProgressDialog.setMax(MAX_PROGRESS); |
06 | mProgressDialog.setButton( "确定" , new DialogInterface.OnClickListener() { |
07 | public void onClick(DialogInterface dialog, int whichButton) { |
11 | mProgressDialog.setButton2( "取消" , new DialogInterface.OnClickListener() { |
12 | public void onClick(DialogInterface dialog, int whichButton) { |
16 | mProgressDialog.show(); |
17 | new Thread( this ).start(); |
21 | while (Progress < MAX_PROGRESS) { |
25 | mProgressDialog.incrementProgressBy( 1 ); |
26 | } catch (InterruptedException e) { |
6.多项选择列表框
MultiChoiceID 用于记录多选选中的id号 存在ArrayList中
选中后 add 进ArrayList
取消选中后 remove 出ArrayList。
01 | ArrayListMultiChoiceID = new ArrayList(); |
03 | AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog. this ); |
05 | MultiChoiceID.clear(); |
07 | builder.setIcon(R.drawable.icon); |
08 | builder.setTitle( "多项选择" ); |
09 | builder.setMultiChoiceItems(mItems, |
10 | new boolean []{ false , false , false , false , false , false , false }, |
11 | new DialogInterface.OnMultiChoiceClickListener() { |
12 | public void onClick(DialogInterface dialog, int whichButton, |
15 | MultiChoiceID.add(whichButton); |
16 | showDialog( "你选择的id为" + whichButton + " , " + mItems[whichButton]); |
18 | MultiChoiceID.remove(whichButton); |
23 | builder.setPositiveButton( "确定" , new DialogInterface.OnClickListener() { |
24 | public void onClick(DialogInterface dialog, int whichButton) { |
26 | int size = MultiChoiceID.size(); |
27 | for ( int i = 0 ;i < size; i++) { |
28 | str+= mItems[MultiChoiceID.get(i)] + ", " ; |
30 | showDialog( "你选择的是" + str); |
33 | builder.setNegativeButton( "取消" , new DialogInterface.OnClickListener() { |
34 | public void onClick(DialogInterface dialog, int whichButton) { |
38 | builder.create().show(); |
7.自定义布局
讲到自定义布局我就得多说一说了,为什么要多说一说呢?
其实自定义布局在Android的开发中非常重要 因为它能让开发者做出自己五彩缤纷的Activity 而不用去使用系统枯燥的界面。
自定义dialog有什么好处?
比如我们在开发过长当中 要通过介绍系统发送的一个广播弹出一个dialog . 但是dialog必需是基于activity才能呈现出来 如果没有activity 的话 程序就会崩溃。所以我们可以写一个自定义的 dialog 把它定义成一个activity
这样我们收到一条打开dialog的广播后 直接启动这个 activity 程序正常运行~~
这就是自定义dialog的好处。
注明:下面这个例子只是写了自定义dialog 没有把它单独的写在一个activity中 如果须要的话 可以自己改一下。
01 | AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog. this ); |
02 | LayoutInflater factory = LayoutInflater.from( this ); |
03 | final View textEntryView = factory.inflate(R.layout.test, null ); |
04 | builder.setIcon(R.drawable.icon); |
05 | builder.setTitle( "自定义输入框" ); |
06 | builder.setView(textEntryView); |
07 | builder.setPositiveButton( "确定" , new DialogInterface.OnClickListener() { |
08 | public void onClick(DialogInterface dialog, int whichButton) { |
10 | EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName); |
11 | EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord); |
12 | showDialog( "姓名 :" + userName.getText().toString() + "密码:" + password.getText().toString() ); |
15 | builder.setNegativeButton( "取消" , new DialogInterface.OnClickListener() { |
16 | public void onClick(DialogInterface dialog, int whichButton) { |
20 | builder.create().show(); |
8.读取进度框
显示一个正在转圈的进度条loading
1 | mProgressDialog = new ProgressDialog( this ); |
2 | mProgressDialog.setTitle( "读取ing" ); |
3 | mProgressDialog.setMessage( "正在读取中请稍候" ); |
4 | mProgressDialog.setIndeterminate( true ); |
5 | mProgressDialog.setCancelable( true ); |
6 | mProgressDialog.show(); |
最后如果你还是觉得我写的不够详细 不要紧我把源代码的下载地址贴出来 欢迎大家一起讨论学习 雨松MOMO希望可以和大家一起进步。