Android的AlertDialog详解

转自:http://www.2cto.com/kf/201205/131876.html

[java]  view plain  copy
 print ?
  1.   

参考文章:http://www.cnblogs.com/jiezzy/archive/2012/09/20/2694917.html

                    http://blog.csdn.net/lizzy115/article/details/6924416

AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。

要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。

使用AlertDialog.Builder创建对话框需要了解以下几个方法:

setTitle :为对话框设置标题setIcon :为对话框设置图标setMessage:为对话框设置内容setView : 给对话框设置自定义样式setItems :设置对话框要显示的一个list,一般用于显示几个命令时setMultiChoiceItems :用来设置对话框显示一系列的复选框setNeutralButton    :普通按钮

setPositiveButton   :给对话框添加"Yes"按钮

setNegativeButton :对话框添加"No"按钮

create : 创建对话框

show :显示对话框

一、简单的AlertDialog

下面,创建一个简单的ALertDialog并显示它:

[java]  view plain  copy
 print ?
  1. import android.app.Activity;   
  2. import android.app.AlertDialog;   
  3. import android.app.Dialog;   
  4. import android.os.Bundle;   
  5.    
  6. public class Dialog_AlertDialogDemoActivity extends Activity {   
  7.     /** Called when the activity is first created. */   
  8.     @Override   
  9.     public void onCreate(Bundle savedInstanceState) {   
  10.         super.onCreate(savedInstanceState);   
  11.         setContentView(R.layout.main);   
  12.    
  13.         Dialog alertDialog = new AlertDialog.Builder(this).   
  14.                 setTitle("对话框的标题").   
  15.                 setMessage("对话框的内容").   
  16.                 setIcon(R.drawable.ic_launcher).   
  17.                 create();   
  18.         alertDialog.show();   
  19.     }   
  20. }   

运行结果如下:


二、带按钮的AlertDialog

上面的例子很简单,下面我们在这个AlertDialog上面加几个Button,实现删除操作的提示对话框

[java]  view plain  copy
 print ?
  1. import android.app.Activity;   
  2. import android.app.AlertDialog;   
  3. import android.app.Dialog;   
  4. import android.content.DialogInterface;   
  5. import android.os.Bundle;   
  6.    
  7. public class Dialog_AlertDialogDemoActivity extends Activity {   
  8.     /** Called when the activity is first created. */   
  9.     @Override   
  10.     public void onCreate(Bundle savedInstanceState) {   
  11.         super.onCreate(savedInstanceState);   
  12.         setContentView(R.layout.main);   
  13.    
  14.         Dialog alertDialog = new AlertDialog.Builder(this).   
  15.                 setTitle("确定删除?").   
  16.                 setMessage("您确定删除该条信息吗?").   
  17.                 setIcon(R.drawable.ic_launcher).   
  18.                 setPositiveButton("确定"new DialogInterface.OnClickListener() {   
  19.                        
  20.                     @Override   
  21.                     public void onClick(DialogInterface dialog, int which) {   
  22.                         // TODO Auto-generated method stub    
  23.                     }   
  24.                 }).   
  25.                 setNegativeButton("取消"new DialogInterface.OnClickListener() {   
  26.                        
  27.                     @Override   
  28.                     public void onClick(DialogInterface dialog, int which) {   
  29.                         // TODO Auto-generated method stub    
  30.                     }   
  31.                 }).   
  32.                 setNeutralButton("查看详情"new DialogInterface.OnClickListener() {   
  33.                        
  34.                     @Override   
  35.                     public void onClick(DialogInterface dialog, int which) {   
  36.                         // TODO Auto-generated method stub    
  37.                     }   
  38.                 }).   
  39.                 create();   
  40.         alertDialog.show();   
  41.     }   
  42. }   

运行结果如下:


三、类似ListView的AlertDialog
用setItems(CharSequence[] items, final OnClickListener listener)方法来实现类似ListView的AlertDialog

第一个参数是要显示的数据的数组,第二个参数是点击某个item的触发事件

[java]  view plain  copy
 print ?
  1. import android.app.Activity;   
  2. import android.app.AlertDialog;   
  3. import android.app.Dialog;   
  4. import android.content.DialogInterface;   
  5. import android.os.Bundle;   
  6. import android.widget.Toast;   
  7.    
  8. public class Dialog_AlertDialogDemoActivity extends Activity {   
  9.     /** Called when the activity is first created. */   
  10.     @Override   
  11.     public void onCreate(Bundle savedInstanceState) {   
  12.         super.onCreate(savedInstanceState);   
  13.         setContentView(R.layout.main);   
  14.    
  15.         final String[] arrayFruit = new String[] { "苹果""橘子""草莓""香蕉" };   
  16.    
  17.         Dialog alertDialog = new AlertDialog.Builder(this).   
  18.                 setTitle("你喜欢吃哪种水果?").   
  19.                 setIcon(R.drawable.ic_launcher)   
  20.                 .setItems(arrayFruit, new DialogInterface.OnClickListener() {   
  21.     
  22.                     @Override   
  23.                     public void onClick(DialogInterface dialog, int which) {   
  24.                         Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();   
  25.                     }   
  26.                 }).   
  27.                 setNegativeButton("取消"new DialogInterface.OnClickListener() {   
  28.    
  29.                     @Override   
  30.                     public void onClick(DialogInterface dialog, int which) {   
  31.                         // TODO Auto-generated method stub    
  32.                     }   
  33.                 }).   
  34.                 create();   
  35.         alertDialog.show();   
  36.     }   
  37. }   

运行结果如下:


四、类似RadioButton的AlertDialog
用setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)方法来实现类似RadioButton的AlertDialog

第一个参数是要显示的数据的数组,第二个参数是初始值(初始被选中的item),第三个参数是点击某个item的触发事件

在这个例子里面我们设了一个selectedFruitIndex用来记住选中的item的index

[java]  view plain  copy
 print ?
  1. import android.app.Activity;   
  2. import android.app.AlertDialog;   
  3. import android.app.Dialog;   
  4. import android.content.DialogInterface;   
  5. import android.os.Bundle;   
  6. import android.widget.Toast;   
  7.    
  8. public class Dialog_AlertDialogDemoActivity extends Activity {   
  9.        
  10.     private int selectedFruitIndex = 0;   
  11.        
  12.     /** Called when the activity is first created. */   
  13.     @Override   
  14.     public void onCreate(Bundle savedInstanceState) {   
  15.         super.onCreate(savedInstanceState);   
  16.         setContentView(R.layout.main);   
  17.    
  18.         final String[] arrayFruit = new String[] { "苹果""橘子""草莓""香蕉" };   
  19.    
  20.         Dialog alertDialog = new AlertDialog.Builder(this).   
  21.                 setTitle("你喜欢吃哪种水果?").   
  22.                 setIcon(R.drawable.ic_launcher)   
  23.                 .setSingleChoiceItems(arrayFruit, 0new DialogInterface.OnClickListener() {   
  24.     
  25.                     @Override   
  26.                     public void onClick(DialogInterface dialog, int which) {   
  27.                         selectedFruitIndex = which;   
  28.                     }   
  29.                 }).   
  30.                 setPositiveButton("确认"new DialogInterface.OnClickListener() {   
  31.    
  32.                     @Override   
  33.                     public void onClick(DialogInterface dialog, int which) {   
  34.                         Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();   
  35.                     }   
  36.                 }).   
  37.                 setNegativeButton("取消"new DialogInterface.OnClickListener() {   
  38.    
  39.                     @Override   
  40.                     public void onClick(DialogInterface dialog, int which) {   
  41.                         // TODO Auto-generated method stub    
  42.                     }   
  43.                 }).   
  44.                 create();   
  45.         alertDialog.show();   
  46.     }   
  47. }   

运行结果如下:


五、类似CheckBox的AlertDialog
用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法来实现类似CheckBox的AlertDialog
第一个参数是要显示的数据的数组,第二个参数是选中状态的数组,第三个参数是点击某个item的触发事件

[java]  view plain  copy
 print ?
  1. import android.app.Activity;   
  2. import android.app.AlertDialog;   
  3. import android.app.Dialog;   
  4. import android.content.DialogInterface;   
  5. import android.os.Bundle;   
  6. import android.widget.Toast;   
  7.    
  8. public class Dialog_AlertDialogDemoActivity extends Activity {   
  9.     /** Called when the activity is first created. */   
  10.     @Override   
  11.     public void onCreate(Bundle savedInstanceState) {   
  12.         super.onCreate(savedInstanceState);   
  13.         setContentView(R.layout.main);   
  14.    
  15.         final String[] arrayFruit = new String[] { "苹果""橘子""草莓""香蕉" };   
  16.         final boolean[] arrayFruitSelected = new boolean[] {truetruefalsefalse};   
  17.    
  18.         Dialog alertDialog = new AlertDialog.Builder(this).   
  19.                 setTitle("你喜欢吃哪种水果?").   
  20.                 setIcon(R.drawable.ic_launcher)   
  21.                 .setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {   
  22.                        
  23.                     @Override   
  24.                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {   
  25.                         arrayFruitSelected[which] = isChecked;   
  26.                     }   
  27.                 }).   
  28.                 setPositiveButton("确认"new DialogInterface.OnClickListener() {   
  29.    
  30.                     @Override   
  31.                     public void onClick(DialogInterface dialog, int which) {   
  32.                         StringBuilder stringBuilder = new StringBuilder();   
  33.                         for (int i = 0; i < arrayFruitSelected.length; i++) {   
  34.                             if (arrayFruitSelected[i] == true)   
  35.                             {   
  36.                                 stringBuilder.append(arrayFruit[i] + "、");   
  37.                             }   
  38.                         }   
  39.                         Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();   
  40.                     }   
  41.                 }).   
  42.                 setNegativeButton("取消"new DialogInterface.OnClickListener() {   
  43.    
  44.                     @Override   
  45.                     public void onClick(DialogInterface dialog, int which) {   
  46.                         // TODO Auto-generated method stub    
  47.                     }   
  48.                 }).   
  49.                 create();   
  50.         alertDialog.show();   
  51.     }   
  52. }   

运行结果如下:


六、自定义View的AlertDialog
有时候我们不能满足系统自带的AlertDialog风格,就比如说我们要实现一个Login画面,有用户名和密码,这时我们就要用到自定义View的AlertDialog

先创建Login画面的布局文件

[java]  view plain  copy
 print ?
  1. [html] <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:layout_width="fill_parent"   
  4.     android:layout_height="fill_parent"   
  5.     android:orientation="vertical" >   
  6.    
  7.     <LinearLayout   
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:gravity="center" >   
  11.    
  12.         <TextView   
  13.             android:layout_width="0dip"   
  14.             android:layout_height="wrap_content"   
  15.             android:layout_weight="1"   
  16.             android:text="@string/user" />   
  17.    
  18.         <EditText   
  19.             android:layout_width="0dip"   
  20.             android:layout_height="wrap_content"   
  21.             android:layout_weight="1" />   
  22.     </LinearLayout>   
  23.    
  24.     <LinearLayout   
  25.         android:layout_width="fill_parent"   
  26.         android:layout_height="wrap_content"   
  27.         android:gravity="center" >   
  28.    
  29.         <TextView   
  30.             android:layout_width="0dip"   
  31.             android:layout_height="wrap_content"   
  32.             android:layout_weight="1"   
  33.             android:text="@string/passward" />   
  34.    
  35.         <EditText   
  36.             android:layout_width="0dip"   
  37.             android:layout_height="wrap_content"   
  38.             android:layout_weight="1" />   
  39.     </LinearLayout>   
  40.    
  41. </LinearLayout>   
  42. <?xml version="1.0" encoding="utf-8"?>  
  43. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  44.     android:layout_width="fill_parent"  
  45.     android:layout_height="fill_parent"  
  46.     android:orientation="vertical" >  
  47.   
  48.     <LinearLayout  
  49.         android:layout_width="fill_parent"  
  50.         android:layout_height="wrap_content"  
  51.         android:gravity="center" >  
  52.   
  53.         <TextView  
  54.             android:layout_width="0dip"  
  55.             android:layout_height="wrap_content"  
  56.             android:layout_weight="1"  
  57.             android:text="@string/user" />  
  58.   
  59.         <EditText  
  60.             android:layout_width="0dip"  
  61.             android:layout_height="wrap_content"  
  62.             android:layout_weight="1" />  
  63.     </LinearLayout>  
  64.   
  65.     <LinearLayout  
  66.         android:layout_width="fill_parent"  
  67.         android:layout_height="wrap_content"  
  68.         android:gravity="center" >  
  69.   
  70.         <TextView  
  71.             android:layout_width="0dip"  
  72.             android:layout_height="wrap_content"  
  73.             android:layout_weight="1"  
  74.             android:text="@string/passward" />  
  75.   
  76.         <EditText  
  77.             android:layout_width="0dip"  
  78.             android:layout_height="wrap_content"  
  79.             android:layout_weight="1" />  
  80.     </LinearLayout>  
  81.   
  82. </LinearLayout>  
  83. 然后在Activity里面把Login画面的布局文件添加到AlertDialog上  
  84.   
  85. [java] package com.tianjf;   
  86.    
  87. import android.app.Activity;   
  88. import android.app.AlertDialog;   
  89. import android.app.Dialog;   
  90. import android.content.DialogInterface;   
  91. import android.os.Bundle;   
  92. import android.view.LayoutInflater;   
  93. import android.view.View;   
  94.    
  95. public class Dialog_AlertDialogDemoActivity extends Activity {   
  96.     /** Called when the activity is first created. */   
  97.     @Override   
  98.     public void onCreate(Bundle savedInstanceState) {   
  99.         super.onCreate(savedInstanceState);   
  100.         setContentView(R.layout.main);   
  101.    
  102.         // 取得自定义View    
  103.         LayoutInflater layoutInflater = LayoutInflater.from(this);   
  104.         View myLoginView = layoutInflater.inflate(R.layout.login, null);   
  105.            
  106.         Dialog alertDialog = new AlertDialog.Builder(this).   
  107.                 setTitle("用户登录").   
  108.                 setIcon(R.drawable.ic_launcher).   
  109.                 setView(myLoginView).   
  110.                 setPositiveButton("登录"new DialogInterface.OnClickListener() {   
  111.    
  112.                     @Override   
  113.                     public void onClick(DialogInterface dialog, int which) {   
  114.                         // TODO Auto-generated method stub    
  115.                     }   
  116.                 }).   
  117.                 setNegativeButton("取消"new DialogInterface.OnClickListener() {   
  118.    
  119.                     @Override   
  120.                     public void onClick(DialogInterface dialog, int which) {   
  121.                         // TODO Auto-generated method stub    
  122.                     }   
  123.                 }).   
  124.                 create();   
  125.         alertDialog.show();   
  126.     }   
  127. }   

运行结果如下:

点击打开链接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值