现在的位置: 首页 > 综合 > 正文

Android中的对话框之二:AlertDialog扩展

2013年02月22日 ⁄ 综合 ⁄ 共 4152字 ⁄ 字号 评论关闭

有的时候,可能需要弹出一个对话框,以便从用户的输入来获取某些确认信息。这种情况下,可以重写Activity基类中的受保护方法(protected)onCreateDialog()。

1、创建一个工程:Dialog。

2、main.xml中的代码。

[java] view
plain
copy

  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. <Button  
  8.     android:id="@+id/btn_dialog"  
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"  
  11.     android:text="Click to display a dialog"  
  12.     android:onClick="onClick" />  
  13.    
  14. </LinearLayout>  

3、DialogActivity.java中的代码。

[java] view
plain
copy

  1. package net.horsttnann.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.view.View;  
  9. import android.widget.Toast;  
  10.   
  11. public class DialogActivity extends Activity {  
  12.     CharSequence[] items = { "Google""Apple""Microsoft" };  
  13.     boolean[] itemsChecked = new boolean[items.length];  
  14.   
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.     }  
  21.   
  22.     public void onClick(View v) {  
  23.         showDialog(0);  
  24.     }  
  25.   
  26.     @Override  
  27.     protected Dialog onCreateDialog(int id) {  
  28.         switch (id) {  
  29.         case 0:  
  30.             return new AlertDialog.Builder(this)  
  31.                     .setIcon(R.drawable.ic_launcher)  
  32.                     .setTitle("This is a dialog with some simple text...")  
  33.                     .setPositiveButton("OK",  
  34.                             new DialogInterface.OnClickListener() {  
  35.                                 public void onClick(DialogInterface dialog,  
  36.                                         int whichButton) {  
  37.                                     Toast.makeText(getBaseContext(),  
  38.                                             "OK clicked!", Toast.LENGTH_SHORT)  
  39.                                             .show();  
  40.                                 }  
  41.                             })  
  42.                     .setNegativeButton("Cancel",  
  43.                             new DialogInterface.OnClickListener() {  
  44.                                 public void onClick(DialogInterface dialog,  
  45.                                         int whichButton) {  
  46.                                     Toast.makeText(getBaseContext(),  
  47.                                             "Cancel clicked!",  
  48.                                             Toast.LENGTH_SHORT).show();  
  49.                                 }  
  50.                             })  
  51.                     .setMultiChoiceItems(items, itemsChecked,  
  52.                             new DialogInterface.OnMultiChoiceClickListener() {  
  53.                                 public void onClick(DialogInterface dialog,  
  54.                                         int which, boolean isChecked) {  
  55.                                     Toast.makeText(  
  56.                                             getBaseContext(),  
  57.                                             items[which]  
  58.                                                     + (isChecked ? " checked!"  
  59.                                                             : " unchecked!"),  
  60.                                             Toast.LENGTH_SHORT).show();  
  61.   
  62.                                 }  
  63.                             }).create();  
  64.         }  
  65.         return null;  
  66.     }  
  67. }  

4、调试。

点击按钮弹出对话框,在CheckBox上面打勾,就会弹出一个Toast提示,显示选中物件的文本信息。点击“OK”或“Cancel”按钮会使对话框消失。

想要显示对话框,首先要重写Activity基类中的onCreateDialog()方法:

[java] view
plain
copy

  1. @Override  
  2. protected Dialog onCreateDialog(int id) {  
  3.     // ...  
  4. }  

当调用showDialog()的时候,上面被重写的方法就被调用了:

[java] view
plain
copy

  1. public void onClick(View v) {  
  2.     showDialog(0);  
  3. }  

这个创建对话框的onCreateDialog()方法是一个被Activity控制的回调函数,当调用showDialog()时,onCreateDialog()回调函数就被触发了。showDialog()方法接受一个Integer参数,用来识别到底要显示哪个对话框。一般情况下,使用switch语句去判断显示不同的对话框。

想要创建一个对话框,还需要使用AlertDialog类的Builder构造器,设置不同的属性,比如图标、标题、按钮、单选框等等:

[java] view
plain
copy

  1.     @Override  
  2.     protected Dialog onCreateDialog(int id) {  
  3.         

抱歉!评论已关闭.