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. 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.进度条框


点击进度条框按钮后 开启一个线程计算读取的进度 假设读取结束为 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. 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.多项选择列表框



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

  1. ArrayList <Integer>MultiChoiceID = new ArrayList <Integer>();
  1. AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
  2. MultiChoiceID.clear();
  3. builder.setIcon(R.drawable.icon);
  4. builder.setTitle("多项选择");
  5. builder.setMultiChoiceItems(mItems,
  6. new boolean[]{false, false, false, false, false, false, false},
  7. new DialogInterface.OnMultiChoiceClickListener() {
  8. public void onClick(DialogInterface dialog, int whichButton,
  9. boolean isChecked) {
  10. if(isChecked) {
  11. MultiChoiceID.add(whichButton);
  12. showDialog("你选择的id为" + whichButton + " , " + mItems[whichButton]);
  13. }else {
  14. MultiChoiceID.remove(whichButton);
  15. }
  16. }
  17. });
  18. builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  19. public void onClick(DialogInterface dialog, int whichButton) {
  20. String str = "";
  21. int size = MultiChoiceID.size();
  22. for (int i = 0 ;i < size; i++) {
  23. str+= mItems[MultiChoiceID.get(i)] + ", ";
  24. }
  25. showDialog("你选择的是" + str);
  26. }
  27. });
  28. builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  29. public void onClick(DialogInterface dialog, int whichButton) {
  30. }
  31. });
  32. 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. 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();
  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. <TextView android:layout_height="wrap_content"
  13. android:layout_width="wrap_content"
  14. android:id="@+id/tvUserName"
  15. android:text="姓名:" />
  16. <EditText android:layout_height="wrap_content"
  17. android:layout_width="wrap_content"
  18. android:id="@+id/etUserName"
  19. android:minWidth="200dip"/>
  20. </LinearLayout>
  21. <LinearLayout
  22. android:layout_height="wrap_content"
  23. android:layout_width="wrap_content"
  24. android:orientation="horizontal"
  25. android:id="@+id/dialognum"
  26. android:layout_below="@+id/dialogname"
  27. >
  28. <TextView android:layout_height="wrap_content"
  29. android:layout_width="wrap_content"
  30. android:id="@+id/tvPassWord"
  31. android:text="密码:" />
  32. <EditText android:layout_height="wrap_content"
  33. android:layout_width="wrap_content"
  34. android:id="@+id/etPassWord"
  35. android:minWidth="200dip"/>
  36. </LinearLayout>
  37. </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使用技巧合集-转载的更多相关文章

  1. android中提示&对话框----AlertDialog

    AlertDialog(对话框) 一.对话框的基本使用流程 step1:创建AlertDialog.Buider; step2:调用setIcon()设置图标,setTitle()或者setCusto ...

  2. Android中UI设计的一些技巧!!!

    出处:http://blog.csdn.net/android_tutor/article/details/5995759 大家好,今天给大家分享的是Android中UI设计的一些技巧,本节内容主要有 ...

  3. Android详细的对话框AlertDialog.Builder使用方法

      我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继 ...

  4. Android中Dialog对话框的调用及监听

    Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter, ...

  5. Android中Dialog对话框

    布局文件xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  6. android 中打 Log 的一些技巧

    在 android 平台上搞开发工作,会经常用到一些 Log 输出调试信息. 众所周知,android 中有五种类型的 Log , v, d, i, w, e 这里就不再赘 述 (如果对这些不了解的朋 ...

  7. android中常见对话框之一AlertDialog

    在Android应用中,有多种对话框:Dialog.AlertDialog.ProgressDialog.时间.日期等对话框. (1)Dialog类,是一切对话框的基类,需要注意的是,Dialog类虽 ...

  8. 11.Android之常用对话框AlertDialog学习

    (1)首先我们写个简单的AlertDialog对话框,要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法,然后创建对话框可以设置对话框的属性,比如 ...

  9. android中提示&对话框----ProgressDialog&DatePickerDialog &TimePickerDialog&PopupWindow

    ProgressDialog(精度条对话框): 1.直接调用ProgressDialog提供的静态方法show()显示 2.创建ProgressDialog,再设置对话框的参数,最后show()出来 ...

随机推荐

  1. asp.net学习视频资料地址链接

    ASP.NET开发学习视频教程大全(共800集) http://felix520wj.blog.51cto.com/7129746/1548458 http://study.163.com/cours ...

  2. 一、链接Sql Server2014提示找不到实例的问题解决方案

    在登录数据库时,确认数据库地址.用户名.密码正确的情况下,却报如下错误,则说明目标数据库服务器有相应的服务未启动. 在目标数据库服务器中打开服务列表: 找到SQL Server(****)服务(括号中 ...

  3. Python之线程&进程

    线程: 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务. thr ...

  4. Gentoo解决Windows双系统时间不同步的问题

    升级后的gentoo不再使用/etc/conf.d/clock,而是使用/etc/conf.d/hwclock来设置和时间相关了. 在/etc/conf.d/hwclock文件中设置系统时间为本地时间 ...

  5. Pots(BFS)

    Pots Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submiss ...

  6. ajax的项目实操(只用于记录部分文件未引入)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  7. SQL 课程

    今天,我主要学习了数据库的基本查询,模糊查询.排序查询.聚合函数.计数和分组,以及数学函数.字符串函数.时间日期函数. create database lianxi0720gouse lianxi07 ...

  8. KVM 命令行启动第一台虚拟机

    KVM创建第一台虚拟机 1 创建一个镜像 [root@kvm ~]# qemu-img create -f raw /opt/CentOS6.-x86_64.raw 5G Formatting [ro ...

  9. Java对象的强、软、弱和虚引用原理+结合ReferenceQueue对象构造Java对象的高速缓存器

    //转 http://blog.csdn.net/lyfi01/article/details/6415726 1.Java对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变 ...

  10. html5 画个圈

    <html><body><canvas id="myCanvas" width="200" height="100&qu ...