在一个Activity中使用多个Dialog

        Android Dev-Guide 推荐重写Activity.onCreateDialog()方法来创建Dialog,这样Dialog就归属于这个Activity了。使用方法是这样的,Activity.showDialog()激发Activity.onCreateDialog()创建Dialog,然后显示之,便于多个Dialog的统一管理。注意,以后再用Activity.showDialog()显示同一个Dialog时,则不会调用Activity.onCreateDialog(),而是调用Activity.onPrepareDialog(),使用上一次显示Dialog时的状态。即 
    第一次:showDialog() -> onCreatedialog() 
        以后: showDialog() -> onPrepareDialog() 

        在示例代码中,分别用createExitDialog(),createListDialog(),createRadioDialog(),createCheckboxDialog(),创建4种Dialog,并在Activity中显示。示例代码如下:
 

Java代码  收藏代码
  1. package com.ipjmc.dialog;  
  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.util.Log;  
  9. import android.widget.Toast;  
  10.   
  11. public class ShowDialogActivity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     public static final String TAG = "ShowDialog";  
  14.     public static final int ID_EXIT_DIALOG = 1;  
  15.     public static final int ID_LIST_DIALOG = 2;  
  16.     public static final int ID_RADIO_DIALOG = 3;  
  17.     public static final int ID_CHECKBOX_DIALOG = 4;  
  18.       
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.           
  24.         showDialog(ID_EXIT_DIALOG);  
  25.         showDialog(ID_LIST_DIALOG);  
  26.         showDialog(ID_RADIO_DIALOG);  
  27.         showDialog(ID_CHECKBOX_DIALOG);  
  28.     }  
  29.       
  30.     @Override  
  31.     protected Dialog onCreateDialog(int id) {  
  32.         // TODO Auto-generated method stub  
  33.         Dialog dialog = null;  
  34.         switch(id) {  
  35.         case ID_EXIT_DIALOG :  
  36.             dialog = createExitDialog();  
  37.             break;  
  38.         case ID_LIST_DIALOG :  
  39.             dialog = createListDialog();  
  40.             break;  
  41.         case ID_RADIO_DIALOG :  
  42.             dialog = createRadioDialog();  
  43.             break;  
  44.         case ID_CHECKBOX_DIALOG :  
  45.             dialog = createCheckboxDialog();  
  46.             break;  
  47.         default :  
  48.             break;  
  49.         }  
  50.         if (dialog != null) {  
  51.             Log.i(TAG, dialog.toString());  
  52.         } else {  
  53.             Log.i(TAG, "dialog = null");  
  54.         }  
  55.         return dialog;  
  56.     }  
  57.       
  58.     @Override  
  59.     protected void onPrepareDialog(int id, Dialog dialog) {  
  60.         // TODO Auto-generated method stub  
  61.         super.onPrepareDialog(id, dialog);  
  62.     }  
  63.   
  64.     //创建简单Dialog  
  65.     public Dialog createExitDialog() {  
  66.         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  67.         builder.setMessage("Are you sure you want to exit?")  
  68.                .setCancelable(false)  
  69.                .setPositiveButton("Yes"new DialogInterface.OnClickListener() {  
  70.                    public void onClick(DialogInterface dialog, int id) {  
  71.                         ShowDialogActivity.this.finish();  
  72.                    }  
  73.                })  
  74.                .setNegativeButton("No"new DialogInterface.OnClickListener() {  
  75.                    public void onClick(DialogInterface dialog, int id) {  
  76.                         dialog.cancel();  
  77.                    }  
  78.                });  
  79.         return builder.create();  
  80.     }  
  81.   
  82.     //创建ListDialog  
  83.     public Dialog createListDialog() {  
  84.         final CharSequence[] items = {"Red""Green""Blue"};  
  85.   
  86.         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  87.         builder.setTitle("Pick a color");  
  88.         builder.setItems(items, new DialogInterface.OnClickListener() {  
  89.             public void onClick(DialogInterface dialog, int item) {  
  90.                 Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();  
  91.             }  
  92.         });  
  93.         return builder.create();  
  94.     }  
  95.   
  96.     //创建单选Dialog  
  97.     public Dialog createRadioDialog() {  
  98.         final CharSequence[] items = {"Red""Green""Blue"};  
  99.   
  100.         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  101.         builder.setTitle("Pick a color");  
  102.         builder.setSingleChoiceItems(items, -1new DialogInterface.OnClickListener() {  
  103.             public void onClick(DialogInterface dialog, int position) {  
  104.                 Toast.makeText(getApplicationContext(), position + " -> " + items[position], Toast.LENGTH_SHORT).show();  
  105.                 dialog.dismiss();  
  106.             }  
  107.         });  
  108.         return builder.create();  
  109.     }  
  110.   
  111.     //创建多选Dialog  
  112.     public Dialog createCheckboxDialog() {  
  113.         final CharSequence[] items = {"Red""Green""Blue"};  
  114.         final boolean [] checked = new boolean [] {falsefalsefalse};  
  115.           
  116.         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
  117.         builder.setTitle("Pick a color");  
  118.         builder.setMultiChoiceItems(items, checked, new DialogInterface.OnMultiChoiceClickListener() {  
  119.               
  120.             @Override  
  121.             public void onClick(DialogInterface dialog, int which, boolean isChecked) {  
  122.                 // TODO Auto-generated method stub  
  123.                   
  124.             }  
  125.         });  
  126.           
  127.         builder.setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  128.                public void onClick(DialogInterface dialog, int id) {  
  129.                     ShowDialogActivity.this.finish();  
  130.                }  
  131.            })  
  132.            .setNegativeButton("取消"new DialogInterface.OnClickListener() {  
  133.                public void onClick(DialogInterface dialog, int id) {  
  134.                     dialog.cancel();  
  135.                }  
  136.            });  
  137.           
  138.         return builder.create();  
  139.      
  140.     }  
  141. }  

你可能感兴趣的:(Android学习笔记)