Android--AlertDialog的用法

来源:互联网 发布:剑灵人女捏脸数据图表 编辑:程序博客网 时间:2024/05/16 13:46

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 plaincopy
  1. package com.tianjf;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.Dialog;  
  6. import android.os.Bundle;  
  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.         Dialog alertDialog = new AlertDialog.Builder(this).  
  16.                 setTitle("对话框的标题").  
  17.                 setMessage("对话框的内容").  
  18.                 setIcon(R.drawable.ic_launcher).  
  19.                 create();  
  20.         alertDialog.show();  
  21.     }  
  22. }  
运行结果如下:


二、带按钮的AlertDialog

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

[java] view plaincopy
  1. package com.tianjf;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.Dialog;  
  6. import android.content.DialogInterface;  
  7. import android.os.Bundle;  
  8.   
  9. public class Dialog_AlertDialogDemoActivity extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.   
  16.         Dialog alertDialog = new AlertDialog.Builder(this).  
  17.                 setTitle("确定删除?").  
  18.                 setMessage("您确定删除该条信息吗?").  
  19.                 setIcon(R.drawable.ic_launcher).  
  20.                 setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  21.                       
  22.                     @Override  
  23.                     public void onClick(DialogInterface dialog, int which) {  
  24.                         // TODO Auto-generated method stub  
  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.                 setNeutralButton("查看详情"new DialogInterface.OnClickListener() {  
  35.                       
  36.                     @Override  
  37.                     public void onClick(DialogInterface dialog, int which) {  
  38.                         // TODO Auto-generated method stub  
  39.                     }  
  40.                 }).  
  41.                 create();  
  42.         alertDialog.show();  
  43.     }  
  44. }  
在这个例子中,我们定义了三个按钮,分别是"Yes"按钮,"No"按钮以及一个普通按钮,每个按钮都有onClick事件,TODO的地方可以放点了按钮之后想要做的一些处理

看一下运行结果:


可以看到三个按钮添加到了AlertDialog上,三个没有添加事件处理的按钮,点了只是关闭对话框,没有任何其他操作。



三、类似ListView的AlertDialog

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

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

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



四、类似RadioButton的AlertDialog

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

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

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

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

运行结果如下:


五、类似CheckBox的AlertDialog

用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法来实现类似CheckBox的AlertDialog

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

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


六、自定义View的AlertDialog

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

先创建Login画面的布局文件

[html] view plaincopy
  1. <?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>  

然后在Activity里面把Login画面的布局文件添加到AlertDialog上

[java] view plaincopy
  1. package com.tianjf;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.Dialog;  
  6. import android.content.DialogInterface;  
  7. import android.os.Bundle;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10.   
  11. public class Dialog_AlertDialogDemoActivity extends Activity {  
  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.         // 取得自定义View  
  19.         LayoutInflater layoutInflater = LayoutInflater.from(this);  
  20.         View myLoginView = layoutInflater.inflate(R.layout.login, null);  
  21.           
  22.         Dialog alertDialog = new AlertDialog.Builder(this).  
  23.                 setTitle("用户登录").  
  24.                 setIcon(R.drawable.ic_launcher).  
  25.                 setView(myLoginView).  
  26.                 setPositiveButton("登录"new DialogInterface.OnClickListener() {  
  27.   
  28.                     @Override  
  29.                     public void onClick(DialogInterface dialog, int which) {  
  30.                         // TODO Auto-generated method stub  
  31.                     }  
  32.                 }).  
  33.                 setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  34.   
  35.                     @Override  
  36.                     public void onClick(DialogInterface dialog, int which) {  
  37.                         // TODO Auto-generated method stub  
  38.                     }  
  39.                 }).  
  40.                 create();  
  41.         alertDialog.show();  
  42.     }  
  43. }  
运行结果如下:



Dialog的重用:

有可能同一个画面有多次机会要谈出DatePickerDialog(例如注册画面),但是如果每次都new一个DatePickerDialog的话,是不可取的,会消耗内存。

Android为DatePickerDialog提供了updateDate方法来解决此问题,也就是共用一个DatePickerDialog,只要更新一下显示的时间就OK了。

[java] view plaincopy
  1.     @Override  
  2.     protected Dialog onCreateDialog(int id) {  
  3.         switch (id) {  
  4.         case DIALOG_ID_DATE:  
  5.             // 时间选择Dialog  
  6.             return new DatePickerDialog(this, mDateSetListener, mDateYear, mDateMonth, mDateDay);  
  7.   
  8.         default:  
  9.             break;  
  10.         }  
  11.         return super.onCreateDialog(id);  
  12.     }  
  13.   
  14.     @Override  
  15.     protected void onPrepareDialog(int id, Dialog dialog) {  
  16.         switch (id) {  
  17.         case DIALOG_ID_DATE:  
  18.             // 时间选择Dialog              
  19.             ((DatePickerDialog) dialog).<span style="color:#CC0000;">updateDate</span>(mDateYear, mDateMonth, mDateDay);  
  20.             dialog.setOnDismissListener(new OnDismissListener() {  
  21.                 @Override  
  22.                 public void onDismiss(DialogInterface dialog) {  
  23.                     // 时间控件的背景设置  
  24.                     mClickText.setBackgroundResource(R.drawable.period_start_date_pink01);  
  25.                 }  
  26.             });  
  27.             break;  
  28.   
  29.         default:  
  30.             break;  
  31.         }  
  32.     }  

当重复调用的时候,不需要重新showDialog(DIALOG_ID_DATE);

只要在onPrepareDialog方法(onPrepareDialog在dialog每次出现时调用,而onCreateDialog只在dialog第一次创建时调用)里面调用updateDate方法更新时间即可


但是当我们自定义的dialog怎么办呢,没有提供类似的更新数据的方法啊。

那没有办法,只能先removeDialog(DIALOG_ID_DO_DELETE); 再 showDialog(DIALOG_ID_DO_DELETE);了
原创粉丝点击