Android 所有Dialog大合集

来源:互联网 发布:用最小二乘法处理数据 编辑:程序博客网 时间:2024/04/30 22:32
今天我用自己写的一个Demo 和大家详细介绍一个Android中的对话框的使用技巧
a.jpg
2011-12-12 22:38 上传
下载附件(36.88 KB)
1.确定取消对话框

对话框中有2个按钮   通过调用setPositiveButton 方法 和setNegativeButton 方法 可以设置按钮的显示内容以及按钮的监听事件
b.jpg
2011-12-12 22:38 上传
下载附件(42.61 KB)
我们使用AlerDialog 创建对话框


使用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();
复制代码


2.多个按钮信息框
c.jpg
2011-12-12 22:38 上传
下载附件(38.76 KB)
d.jpg
2011-12-12 22:38 上传
下载附件(31.18 KB)


  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.列表框
e.jpg
2011-12-12 22:38 上传
下载附件(30.46 KB)
这个数组用于列表选择


  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.单项选择列表框
f.jpg
2011-12-12 22:38 上传
下载附件(36.11 KB)
mSingleChoice 用于记录单选中的ID



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

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

  20. }
  21. });
  22. builder.create().show();
复制代码
5.进度条框
h.jpg
2011-12-12 22:38 上传
下载附件(41.11 KB)
点击进度条框按钮后 开启一个线程计算读取的进度 假设读取结束为 100Progress在小于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. ic void run() {
  19. int Progress = 0;
  20. while(Progress < MAX_PROGRESS) {
  21. try {
  22. Thread.sleep(100);
  23. Progress++;
  24. mProgressDialog.incrementProgressBy(1);
  25. } catch (InterruptedException e) {
  26. // TODO Auto-generated catch block
  27. e.printStackTrace();
  28. }

  29. }
复制代码
6.多项选择列表框
i.jpg
2011-12-12 22:38 上传
下载附件(36.62 KB)
j.jpg
2011-12-12 22:38 上传
下载附件(32.1 KB)
MultiChoiceID 用于记录多选选中的id号 存在ArrayList
选中后 add ArrayList
取消选中后 remove ArrayList

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

  2. view plaincopy to clipboardprint?
  3. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);

  4. MultiChoiceID.clear();
  5. builder.setIcon(R.drawable.icon);
  6. builder.setTitle("多项选择");
  7. builder.setMultiChoiceItems(mItems,
  8. new boolean[]{false, false, false, false, false, false, false},
  9. new DialogInterface.OnMultiChoiceClickListener() {
  10. public void onClick(DialogInterface dialog, int whichButton,
  11. boolean isChecked) {
  12. if(isChecked) {
  13. MultiChoiceID.add(whichButton);
  14. showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);
  15. }else {
  16. MultiChoiceID.remove(whichButton);
  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. builder.create().show();
复制代码


7.自定义布局
k.jpg
2011-12-12 22:38 上传
下载附件(41.36 KB)
讲到自定义布局我就得多说一说了,为什么要多说一说呢? 其实自定义布局在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. EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);
  10. EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);
  11. showDialog("姓名 :" + userName.getText().toString() + "密码:" + password.getText().toString() );
  12. }
  13. });
  14. builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  15. public void onClick(DialogInterface dialog, int whichButton) {

  16. }
  17. });
  18. builder.create().show();

  19. view plaincopy to clipboardprint?
  20. <span style="color:#000000;"><?xml version="1.0" encoding="utf-8"?>
  21. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  22. android:layout_height="wrap_content"
  23. android:layout_width="wrap_content"
  24. android:orientation="horizontal"
  25. android:id="@+id/dialog">
  26. <LinearLayout
  27. android:layout_height="wrap_content"
  28. android:layout_width="wrap_content"
  29. android:orientation="horizontal"
  30. android:id="@+id/dialogname">

  31. <TextView android:layout_height="wrap_content"
  32. android:layout_width="wrap_content"
  33. android:id="@+id/tvUserName"
  34. android:text="姓名:" />
  35. <EditText android:layout_height="wrap_content"
  36. android:layout_width="wrap_content"
  37. android:id="@+id/etUserName"
  38. android:minWidth="200dip"/>
  39. </LinearLayout>
  40. <LinearLayout
  41. android:layout_height="wrap_content"
  42. android:layout_width="wrap_content"
  43. android:orientation="horizontal"
  44. android:id="@+id/dialognum"
  45. android:layout_below="@+id/dialogname"
  46. >
  47. <TextView android:layout_height="wrap_content"
  48. android:layout_width="wrap_content"
  49. android:id="@+id/tvPassWord"
  50. android:text="密码:" />
  51. <EditText android:layout_height="wrap_content"
  52. android:layout_width="wrap_content"
  53. android:id="@+id/etPassWord"
  54. android:minWidth="200dip"/>
  55. </LinearLayout>
  56. </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();
复制代码

























h.jpg(41.11 KB, 下载次数: 40)

2011-12-12 22:38 上传

点击文件名下载附件

h.jpg