Android对话框


一、Dialog的创建

1、继承Activity ,重写onCreateDialog方法

  1. public class AlertDialogSamples extends Activity {  
  2.     private static final int DIALOG_YES_NO_MESSAGE = 1;  
  3.     private static final int DIALOG_YES_NO_LONG_MESSAGE = 2;  
  4.     private static final int DIALOG_LIST = 3;  
  5.     private static final int DIALOG_PROGRESS = 4;  
  6.     private static final int DIALOG_SINGLE_CHOICE = 5;  
  7.     private static final int DIALOG_MULTIPLE_CHOICE = 6;  
  8.     private static final int DIALOG_TEXT_ENTRY = 7;  
  9.   
  10.     private static final int MAX_PROGRESS = 100;  
  11.       
  12.     private ProgressDialog mProgressDialog;  
  13.     private int mProgress;  
  14.     private Handler mProgressHandler;  
  15.   
  16.     @Override  
  17.     protected Dialog onCreateDialog(int id) {  
  18.         switch (id) {  
  19.         case DIALOG_YES_NO_MESSAGE:  
  20.             return new AlertDialog.Builder(AlertDialogSamples.this)  
  21.                 .setIcon(R.drawable.alert_dialog_icon)  
  22.                 .setTitle(R.string.alert_dialog_two_buttons_title)  
  23.                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  24.                     public void onClick(DialogInterface dialog, int whichButton) {  
  25.   
  26.                         /* User clicked OK so do some stuff */  
  27.                     }  
  28.                 })  
  29.                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  30.                     public void onClick(DialogInterface dialog, int whichButton) {  
  31.   
  32.                         /* User clicked Cancel so do some stuff */  
  33.                     }  
  34.                 })  
  35.                 .create();  
  36.         case DIALOG_YES_NO_LONG_MESSAGE:  
  37.             return new AlertDialog.Builder(AlertDialogSamples.this)  
  38.                 .setIcon(R.drawable.alert_dialog_icon)  
  39.                 .setTitle(R.string.alert_dialog_two_buttons_msg)  
  40.                 .setMessage(R.string.alert_dialog_two_buttons2_msg)  
  41.                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  42.                     public void onClick(DialogInterface dialog, int whichButton) {  
  43.       
  44.                         /* User clicked OK so do some stuff */  
  45.                     }  
  46.                 })  
  47.                 .setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() {  
  48.                     public void onClick(DialogInterface dialog, int whichButton) {  
  49.   
  50.                         /* User clicked Something so do some stuff */  
  51.                     }  
  52.                 })  
  53.                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  54.                     public void onClick(DialogInterface dialog, int whichButton) {  
  55.   
  56.                         /* User clicked Cancel so do some stuff */  
  57.                     }  
  58.                 })  
  59.                 .create();  
  60.         case DIALOG_LIST:  
  61.             return new AlertDialog.Builder(AlertDialogSamples.this)  
  62.                 .setTitle(R.string.select_dialog)  
  63.                 .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {  
  64.                     public void onClick(DialogInterface dialog, int which) {  
  65.   
  66.                         /* User clicked so do some stuff */  
  67.                         String[] items = getResources().getStringArray(R.array.select_dialog_items);  
  68.                         new AlertDialog.Builder(AlertDialogSamples.this)  
  69.                                 .setMessage("You selected: " + which + " , " + items[which])  
  70.                                 .show();  
  71.                     }  
  72.                 })  
  73.                 .create();  
  74.         case DIALOG_PROGRESS:  
  75.             mProgressDialog = new ProgressDialog(AlertDialogSamples.this);  
  76.             mProgressDialog.setIcon(R.drawable.alert_dialog_icon);  
  77.             mProgressDialog.setTitle(R.string.select_dialog);  
  78.             mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  79.             mProgressDialog.setMax(MAX_PROGRESS);  
  80.             mProgressDialog.setButton(getText(R.string.alert_dialog_hide), new DialogInterface.OnClickListener() {  
  81.                 public void onClick(DialogInterface dialog, int whichButton) {  
  82.   
  83.                     /* User clicked Yes so do some stuff */  
  84.                 }  
  85.             });  
  86.             mProgressDialog.setButton2(getText(R.string.alert_dialog_cancel), new DialogInterface.OnClickListener() {  
  87.                 public void onClick(DialogInterface dialog, int whichButton) {  
  88.   
  89.                     /* User clicked No so do some stuff */  
  90.                 }  
  91.             });  
  92.             return mProgressDialog;  
  93.         case DIALOG_SINGLE_CHOICE:  
  94.             return new AlertDialog.Builder(AlertDialogSamples.this)  
  95.                 .setIcon(R.drawable.alert_dialog_icon)  
  96.                 .setTitle(R.string.alert_dialog_single_choice)  
  97.                 .setSingleChoiceItems(R.array.select_dialog_items2, 0new DialogInterface.OnClickListener() {  
  98.                     public void onClick(DialogInterface dialog, int whichButton) {  
  99.   
  100.                         /* User clicked on a radio button do some stuff */  
  101.                     }  
  102.                 })  
  103.                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  104.                     public void onClick(DialogInterface dialog, int whichButton) {  
  105.   
  106.                         /* User clicked Yes so do some stuff */  
  107.                     }  
  108.                 })  
  109.                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  110.                     public void onClick(DialogInterface dialog, int whichButton) {  
  111.   
  112.                         /* User clicked No so do some stuff */  
  113.                     }  
  114.                 })  
  115.                .create();  
  116.         case DIALOG_MULTIPLE_CHOICE:  
  117.             return new AlertDialog.Builder(AlertDialogSamples.this)  
  118.                 .setIcon(R.drawable.ic_popup_reminder)  
  119.                 .setTitle(R.string.alert_dialog_multi_choice)  
  120.                 .setMultiChoiceItems(R.array.select_dialog_items3,  
  121.                         new boolean[]{falsetruefalsetruefalsefalsefalse},  
  122.                         new DialogInterface.OnMultiChoiceClickListener() {  
  123.                             public void onClick(DialogInterface dialog, int whichButton,  
  124.                                     boolean isChecked) {  
  125.   
  126.                                 /* User clicked on a check box do some stuff */  
  127.                             }  
  128.                         })  
  129.                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  130.                     public void onClick(DialogInterface dialog, int whichButton) {  
  131.   
  132.                         /* User clicked Yes so do some stuff */  
  133.                     }  
  134.                 })  
  135.                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  136.                     public void onClick(DialogInterface dialog, int whichButton) {  
  137.   
  138.                         /* User clicked No so do some stuff */  
  139.                     }  
  140.                 })  
  141.                .create();  
  142.         case DIALOG_TEXT_ENTRY:  
  143.             // This example shows how to add a custom layout to an AlertDialog   
  144.             LayoutInflater factory = LayoutInflater.from(this);  
  145.             final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);  
  146.             return new AlertDialog.Builder(AlertDialogSamples.this)  
  147.                 .setIcon(R.drawable.alert_dialog_icon)  
  148.                 .setTitle(R.string.alert_dialog_text_entry)  
  149.                 .setView(textEntryView)  
  150.                 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  151.                     public void onClick(DialogInterface dialog, int whichButton) {  
  152.       
  153.                         /* User clicked OK so do some stuff */  
  154.                     }  
  155.                 })  
  156.                 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  157.                     public void onClick(DialogInterface dialog, int whichButton) {  
  158.   
  159.                         /* User clicked cancel so do some stuff */  
  160.                     }  
  161.                 })  
  162.                 .create();  
  163.         }  
  164.         return null;  
  165.     }  
  166.   
  167.     /** 
  168.      * Initialization of the Activity after it is first created.  Must at least 
  169.      * call {@link Android.app.Activity#setContentView(int)} to 
  170.      * describe what is to be displayed in the screen. 
  171.      */  
  172.     @Override  
  173.     protected void onCreate(Bundle savedInstanceState) {  
  174.         super.onCreate(savedInstanceState);  
  175.   
  176.         setContentView(R.layout.alert_dialog);  
  177.                   
  178.         /* Display a text message with yes/no buttons and handle each message as well as the cancel action */  
  179.         Button twoButtonsTitle = (Button) findViewById(R.id.two_buttons);  
  180.         twoButtonsTitle.setOnClickListener(new OnClickListener() {  
  181.             public void onClick(View v) {  
  182.                 showDialog(DIALOG_YES_NO_MESSAGE);  
  183.             }  
  184.         });  
  185.           
  186.         /* Display a long text message with yes/no buttons and handle each message as well as the cancel action */  
  187.         Button twoButtons2Title = (Button) findViewById(R.id.two_buttons2);  
  188.         twoButtons2Title.setOnClickListener(new OnClickListener() {  
  189.             public void onClick(View v) {  
  190.                 showDialog(DIALOG_YES_NO_LONG_MESSAGE);  
  191.             }  
  192.         });  
  193.           
  194.           
  195.         /* Display a list of items */  
  196.         Button selectButton = (Button) findViewById(R.id.select_button);  
  197.         selectButton.setOnClickListener(new OnClickListener() {  
  198.             public void onClick(View v) {  
  199.                 showDialog(DIALOG_LIST);  
  200.             }  
  201.         });  
  202.           
  203.         /* Display a custom progress bar */  
  204.         Button progressButton = (Button) findViewById(R.id.progress_button);  
  205.         progressButton.setOnClickListener(new OnClickListener() {  
  206.             public void onClick(View v) {  
  207.                 showDialog(DIALOG_PROGRESS);  
  208.                 mProgress = 0;  
  209.                 mProgressDialog.setProgress(0);  
  210.                 mProgressHandler.sendEmptyMessage(0);  
  211.             }  
  212.         });  
  213.           
  214.         /* Display a radio button group */  
  215.         Button radioButton = (Button) findViewById(R.id.radio_button);  
  216.         radioButton.setOnClickListener(new OnClickListener() {  
  217.             public void onClick(View v) {  
  218.                 showDialog(DIALOG_SINGLE_CHOICE);  
  219.             }  
  220.         });  
  221.           
  222.         /* Display a list of checkboxes */  
  223.         Button checkBox = (Button) findViewById(R.id.checkbox_button);  
  224.         checkBox.setOnClickListener(new OnClickListener() {  
  225.             public void onClick(View v) {  
  226.                 showDialog(DIALOG_MULTIPLE_CHOICE);  
  227.             }  
  228.         });  
  229.           
  230.         /* Display a text entry dialog */  
  231.         Button textEntry = (Button) findViewById(R.id.text_entry_button);  
  232.         textEntry.setOnClickListener(new OnClickListener() {  
  233.             public void onClick(View v) {  
  234.                 showDialog(DIALOG_TEXT_ENTRY);  
  235.             }  
  236.         });  
  237.           
  238.         mProgressHandler = new Handler() {  
  239.             @Override  
  240.             public void handleMessage(Message msg) {  
  241.                 super.handleMessage(msg);  
  242.                 if (mProgress >= MAX_PROGRESS) {  
  243.                     mProgressDialog.dismiss();  
  244.                 } else {  
  245.                     mProgress++;  
  246.                     mProgressDialog.incrementProgressBy(1);  
  247.                     mProgressHandler.sendEmptyMessageDelayed(0100);  
  248.                 }  
  249.             }  
  250.         };  
  251.     }  
  252. }  

2、直接创建

  与上面方法相似,只不过直接在onClick() 方法或其他方法中直接new。

二、Dialog中的布局

setIcon

图标

setTitle

标题

setMessenge

内容

setItems

Dialog中间显示列示项

setSingleChoiceItems

Dialog中间显示单选项

setMultiChoiceItems

Dialog中间显示复选项

setView

设置Dialog中间的布局,用于自定义Dialog

setPositiveButton

左键

setNeutralButton

中键

setNegativeButton

右键

创建的最后记得要调用Create()和show() 两方法,否则不显示Dialog

相关内容