`
南湖小技工
  • 浏览: 14257 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

<转>Android中的对话框AlertDialog使用技巧合集

阅读更多

 

文章来自:

http://blog.csdn.net/blue6626/article/details/6641105


今天我用自己写的一个Demo 和大家详细介绍一个Android中的对话框的使用技巧。 





1.确定取消对话框


对话框中有2个按钮   通过调用 setPositiveButton 方法 和 setNegativeButton 方法 可以设置按钮的显示内容以及按钮的监听事件
我们使用AlerDialog 创建对话框

  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   

使用builder设置对话框的title button icon 等等

  1. builder.setIcon(R.drawable.icon);  
  2.        builder.setTitle("你确定要离开吗?");  
  3.        builder.setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  4.            public void onClick(DialogInterface dialog, int whichButton) {  
  5.                //这里添加点击确定后的逻辑  
  6.                showDialog("你选择了确定");  
  7.            }  
  8.        });  
  9.        builder.setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  10.            public void onClick(DialogInterface dialog, int whichButton) {  
  11.                //这里添加点击确定后的逻辑  
  12.                showDialog("你选择了取消");  
  13.            }  
  14.        });  
  15.        builder.create().show();  

  这个dialog用于现实onClick后监听的内容信息

  1. private void showDialog(String str) {  
  2. w AlertDialog.Builder(MainDialog.this)  
  3.      .setMessage(str)  
  4.      .show();  
  5. }  



2.多个按钮信息框



  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);           
  2. builder.setIcon(R.drawable.icon);  
  3. builder.setTitle("投票");  
  4. builder.setMessage("您认为什么样的内容能吸引您?");  
  5. builder.setPositiveButton("有趣味的"new DialogInterface.OnClickListener() {  
  6.     public void onClick(DialogInterface dialog, int whichButton) {  
  7.         showDialog("你选择了有趣味的");  
  8.     }  
  9. });  
  10. builder.setNeutralButton("有思想的"new DialogInterface.OnClickListener() {  
  11.     public void onClick(DialogInterface dialog, int whichButton) {  
  12.         showDialog("你选择了有思想的");                      
  13.     }  
  14. });  
  15. builder.setNegativeButton("主题强的"new DialogInterface.OnClickListener() {  
  16.     public void onClick(DialogInterface dialog, int whichButton) {  
  17.         showDialog("你选择了主题强的");    
  18.     }  
  19. });  
  20. builder.create().show();  




3.列表框



这个数组用于列表选择

  1. final String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"};  




  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   
  2.         builder.setTitle("列表选择框");  
  3.         builder.setItems(mItems, new DialogInterface.OnClickListener() {  
  4.             public void onClick(DialogInterface dialog, int which) {  
  5.                 //点击后弹出窗口选择了第几项  
  6.                 showDialog("你选择的id为" + which + " , " + mItems[which]);  
  7.             }  
  8.         });  
  9.         builder.create().show();  



4.单项选择列表框


mSingleChoice 用于记录单选中的ID

  1. int mSingleChoiceID = -1;  
  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   
  2.   
  3. mSingleChoiceID = -1;  
  4. builder.setIcon(R.drawable.icon);  
  5.     builder.setTitle("单项选择");  
  6.     builder.setSingleChoiceItems(mItems, 0new DialogInterface.OnClickListener() {  
  7.         public void onClick(DialogInterface dialog, int whichButton) {  
  8.                 mSingleChoiceID = whichButton;  
  9.                 showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);  
  10.         }  
  11.     });  
  12.     builder.setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  13.         public void onClick(DialogInterface dialog, int whichButton) {  
  14.             if(mSingleChoiceID > 0) {  
  15.             showDialog("你选择的是" + mSingleChoiceID);  
  16.             }  
  17.         }  
  18.     });  
  19.     builder.setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  20.         public void onClick(DialogInterface dialog, int whichButton) {  
  21.   
  22.         }  
  23.     });  
  24.    builder.create().show();  




5.进度条框



点击进度条框按钮后 开启一个线程计算读取的进度 假设读取结束为 100
Progress在小于100的时候一直在线程中做循环++ 只到读取结束后,停止线程。
  1.           mProgressDialog = new ProgressDialog(MainDialog.this);  
  2.      mProgressDialog.setIcon(R.drawable.icon);  
  3.      mProgressDialog.setTitle("进度条窗口");  
  4.      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  5.      mProgressDialog.setMax(MAX_PROGRESS);  
  6.      mProgressDialog.setButton("确定"new DialogInterface.OnClickListener() {  
  7.          public void onClick(DialogInterface dialog, int whichButton) {  
  8.              //这里添加点击后的逻辑  
  9.          }  
  10.      });  
  11.      mProgressDialog.setButton2("取消"new DialogInterface.OnClickListener() {  
  12.          public void onClick(DialogInterface dialog, int whichButton) {  
  13.              //这里添加点击后的逻辑  
  14.          }  
  15.      });  
  16.      mProgressDialog.show();  
  17.      new Thread(this).start();  
  18.   
  19. ic void run() {  
  20. int Progress = 0;  
  21. while(Progress < MAX_PROGRESS) {  
  22. try {  
  23.     Thread.sleep(100);  
  24.     Progress++;    
  25.     mProgressDialog.incrementProgressBy(1);  
  26. catch (InterruptedException e) {  
  27.     // TODO Auto-generated catch block  
  28.     e.printStackTrace();  
  29. }  
  30.    
  31. }  


6.多项选择列表框




MultiChoiceID 用于记录多选选中的id号 存在ArrayList中
选中后 add 进
ArrayList
取消选中后 remove 出ArrayList


  1. ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();  

  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   
  2.   
  3. MultiChoiceID.clear();  
  4. builder.setIcon(R.drawable.icon);  
  5.     builder.setTitle("多项选择");  
  6.     builder.setMultiChoiceItems(mItems,  
  7.             new boolean[]{falsefalsefalsefalsefalsefalsefalse},  
  8.             new DialogInterface.OnMultiChoiceClickListener() {  
  9.                 public void onClick(DialogInterface dialog, int whichButton,  
  10.                         boolean isChecked) {  
  11.                    if(isChecked) {  
  12.                        MultiChoiceID.add(whichButton);  
  13.                        showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);  
  14.                    }else {  
  15.                        MultiChoiceID.remove(whichButton);  
  16.                    }  
  17.                       
  18.                 }  
  19.             });  
  20.     builder.setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  21.         public void onClick(DialogInterface dialog, int whichButton) {  
  22.             String str = "";  
  23.             int size = MultiChoiceID.size();  
  24.             for (int i = 0 ;i < size; i++) {  
  25.             str+= mItems[MultiChoiceID.get(i)] + ", ";  
  26.             }  
  27.             showDialog("你选择的是" + str);  
  28.         }  
  29.     });  
  30.     builder.setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  31.         public void onClick(DialogInterface dialog, int whichButton) {  
  32.   
  33.         }  
  34.     });  
  35.    builder.create().show();  


7.自定义布局


讲到自定义布局我就得多说一说了,为什么要多说一说呢? 
其实自定义布局在Android的开发中非常重要 因为它能让开发者做出自己五彩缤纷的Activity 而不用去使用系统枯燥的界面。

自定义dialog有什么好处?

比如我们在开发过长当中 要通过介绍系统发送的一个广播弹出一个dialog . 但是dialog必需是基于activity才能呈现出来 如果没有activity 的话 程序就会崩溃。所以我们可以写一个自定义的 dialog 把它定义成一个activity
这样我们收到一条打开dialog的广播后 直接启动这个 activity  程序正常运行~~ 

这就是自定义dialog的好处。

注明:下面这个例子只是写了自定义dialog 没有把它单独的写在一个activity中 如果须要的话 可以自己改一下。
  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);   
  2.  LayoutInflater factory = LayoutInflater.from(this);  
  3.  final View textEntryView = factory.inflate(R.layout.test, null);  
  4.      builder.setIcon(R.drawable.icon);  
  5.      builder.setTitle("自定义输入框");  
  6.      builder.setView(textEntryView);  
  7.      builder.setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  8.          public void onClick(DialogInterface dialog, int whichButton) {  
  9.            
  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() );  
  13.          }  
  14.      });  
  15.      builder.setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  16.          public void onClick(DialogInterface dialog, int whichButton) {  
  17.   
  18.          }  
  19.      });  
  20.    builder.create().show();  



  1. <span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:layout_height="wrap_content"   
  4. android:layout_width="wrap_content"  
  5. android:orientation="horizontal"  
  6. android:id="@+id/dialog">  
  7. <LinearLayout  
  8. android:layout_height="wrap_content"   
  9. android:layout_width="wrap_content"  
  10. android:orientation="horizontal"  
  11. android:id="@+id/dialogname">  
  12.   
  13. <TextView android:layout_height="wrap_content"  
  14.    android:layout_width="wrap_content"  
  15.   android:id="@+id/tvUserName"   
  16.   android:text="姓名:" />  
  17. <EditText android:layout_height="wrap_content"  
  18.   android:layout_width="wrap_content"   
  19.   android:id="@+id/etUserName"   
  20.   android:minWidth="200dip"/>  
  21. </LinearLayout>    
  22. <LinearLayout  
  23. android:layout_height="wrap_content"   
  24. android:layout_width="wrap_content"  
  25. android:orientation="horizontal"  
  26. android:id="@+id/dialognum"  
  27.  android:layout_below="@+id/dialogname"  
  28. >  
  29.   <TextView android:layout_height="wrap_content"  
  30.    android:layout_width="wrap_content"  
  31.   android:id="@+id/tvPassWord"   
  32.   android:text="密码:" />  
  33. <EditText android:layout_height="wrap_content"  
  34.   android:layout_width="wrap_content"   
  35.   android:id="@+id/etPassWord"   
  36.   android:minWidth="200dip"/>  
  37.  </LinearLayout>    
  38.   </RelativeLayout></span>  

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();  
源码下载地址:http://www.kuaipan.cn/index.php?ac=file&oid=3166172581218727

 

分享到:
评论

相关推荐

    Android AlertDialog对话框用法示例

    主要介绍了Android AlertDialog对话框用法,结合实例形式分析了AlertDialog对话框的功能及常见使用技巧,需要的朋友可以参考下

    Android实现点击AlertDialog上按钮时不关闭对话框的方法

    主要介绍了Android实现点击AlertDialog上按钮时不关闭对话框的方法,涉及设置监听的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

    Android编程实现AlertDialog自定义弹出对话框的方法示例

    主要介绍了Android编程实现AlertDialog自定义弹出对话框的方法,结合实例形式分析了Android AlertDialog自定义弹出对话框的基本功能与事件监听实现技巧,需要的朋友可以参考下

    Android编程之自定义AlertDialog(退出提示框)用法实例

    主要介绍了Android编程之自定义AlertDialog(退出提示框)用法,结合实例形式较为详细的分析了自定义AlertDialog的页面布局与功能实现相关技巧,需要的朋友可以参考下

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView的交互 3.14 控制不同的文字字体——Typeface对象使用 3.15 如iPhone拖动相片特效——Gallery画廊 3.16 自制计算器——...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView的交互 3.14 控制不同的文字字体——Typeface对象使用 3.15 如iPhone拖动相片特效——Gallery画廊 3.16 自制计算器——...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView的交互 3.14 控制不同的文字字体——Typeface对象使用 3.15 如iPhone拖动相片特效——Gallery画廊 3.16 自制计算器——...

    Android 开发技巧

    0、ANDROID常用类库说明 6 1、ANDROID文件系统与应用程序架构 7 1.1、ANDROID 文件系统 7 1.2、ANDROID应用程序架构 9 2、ANDROID应用程序结构 11 2.1、ACTIVITY 12 2.1.1、概述 12 2.1.2、Activity的生命周期 15 ...

    Android中AlertDilog显示简单和复杂列表的方法

    主要介绍了Android中AlertDialog显示简单和复杂列表的方法,结合实例形式分析了Android的AlertDialog创建列表显示对话框的相关方法与常见操作技巧,需要的朋友可以参考下

    Google Android SDK开发范例大全的目录

    3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView的交互-p60 3.14 控制不同的文字字体——Typeface对象使用 3.15 如iPhone拖动相片特效——Gallery画廊 3.16 自制计算器...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView的交互 3.14 控制不同的文字字体——Typeface对象使用 3.15 如iPhone拖动相片特效——Gallery画廊 3.16 自制计算器...

    Google Android SDK 开发范例大全01

    3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView的交互-p60 3.14 控制不同的文字字体——Typeface对象使用 3.15 如iPhone拖动相片特效——Gallery画廊 3.16 自制计算器...

    Google Android SDK 开发范例大全02

    3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView的交互-p60 3.14 控制不同的文字字体——Typeface对象使用 3.15 如iPhone拖动相片特效——Gallery画廊 3.16 自制计算器...

    Google+Android+SDK开发范例大全

    3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView的交互 3.14 控制不同的文字字体——Typeface对象使用 3.15 如iPhone拖动相片特效——Gallery画廊 3.16 自制计算器——...

    Google Android sdk 开发范例大全 部分章节代码

    3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView的交互-p60 3.14 控制不同的文字字体——Typeface对象使用 3.15 如iPhone拖动相片特效——Gallery画廊 3.16 自制计算器...

    Google Android SDK开发范例大全(完整版)

    3.12 具有交互功能的对话框——AlertDialog窗口 3.13 置换文字颜色的机关——Button与TextView的交互 3.14 控制不同的文字字体——Typeface对象使用 3.15 如iPhone拖动相片特效——Gallery画廊 3.16 自制计算器——...

Global site tag (gtag.js) - Google Analytics