【Android 开发教程】显示普通对话框

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


有的时候,可能需要弹出一个对话框,以便从用户的输入来获取某些确认信息。这种情况下,可以重写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. public class DialogActivity extends Activity {  
  2.     CharSequence[] items = { "Google""Apple""Microsoft" };  
  3.     boolean[] itemsChecked = new boolean[items.length];  
  4.   
  5.     /** Called when the activity is first created. */  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.     }  
  11.   
  12.     public void onClick(View v) {  
  13.         showDialog(0);  
  14.     }  
  15.   
  16.     @Override  
  17.     protected Dialog onCreateDialog(int id) {  
  18.         switch (id) {  
  19.         case 0:  
  20.             return new AlertDialog.Builder(this)  
  21.                     .setIcon(R.drawable.ic_launcher)  
  22.                     .setTitle("This is a dialog with some simple text...")  
  23.                     .setPositiveButton("OK",  
  24.                             new DialogInterface.OnClickListener() {  
  25.                                 public void onClick(DialogInterface dialog,  
  26.                                         int whichButton) {  
  27.                                     Toast.makeText(getBaseContext(),  
  28.                                             "OK clicked!", Toast.LENGTH_SHORT)  
  29.                                             .show();  
  30.                                 }  
  31.                             })  
  32.                     .setNegativeButton("Cancel",  
  33.                             new DialogInterface.OnClickListener() {  
  34.                                 public void onClick(DialogInterface dialog,  
  35.                                         int whichButton) {  
  36.                                     Toast.makeText(getBaseContext(),  
  37.                                             "Cancel clicked!",  
  38.                                             Toast.LENGTH_SHORT).show();  
  39.                                 }  
  40.                             })  
  41.                     .setMultiChoiceItems(items, itemsChecked,  
  42.                             new DialogInterface.OnMultiChoiceClickListener() {  
  43.                                 public void onClick(DialogInterface dialog,  
  44.                                         int which, boolean isChecked) {  
  45.                                     Toast.makeText(  
  46.                                             getBaseContext(),  
  47.                                             items[which]  
  48.                                                     + (isChecked ? " checked!"  
  49.                                                             : " unchecked!"),  
  50.                                             Toast.LENGTH_SHORT).show();  
  51.   
  52.                                 }  
  53.                             }).create();  
  54.         }  
  55.         return null;  
  56.     }  
  57. }  

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.         switch (id) {  
  4.         case 0:  
  5.             Builder builder = new AlertDialog.Builder(this);  
  6.             builder.setIcon(R.drawable.ic_launcher);  
  7.             builder.setTitle("This is a dialog with some simple text...");  
  8.             builder.setPositiveButton("OK",  
  9.                     new DialogInterface.OnClickListener() {  
  10.                         public void onClick(DialogInterface dialog,  
  11.                                 int whichButton) {  
  12.                             Toast.makeText(getBaseContext(), "OK clicked!",  
  13.                                     Toast.LENGTH_SHORT).show();  
  14.                         }  
  15.                     });  
  16.   
  17.             builder.setNegativeButton("Cancel",  
  18.                     new DialogInterface.OnClickListener() {  
  19.                         public void onClick(DialogInterface dialog,  
  20.                                 int whichButton) {  
  21.                             Toast.makeText(getBaseContext(), "Cancel clicked!",  
  22.                                     Toast.LENGTH_SHORT).show();  
  23.                         }  
  24.                     });  
  25.   
  26.             builder.setMultiChoiceItems(items, itemsChecked,  
  27.                     new DialogInterface.OnMultiChoiceClickListener() {  
  28.                         public void onClick(DialogInterface dialog, int which,  
  29.                                 boolean isChecked) {  
  30.                             Toast.makeText(  
  31.                                     getBaseContext(),  
  32.                                     items[which]  
  33.                                             + (isChecked ? " checked!"  
  34.                                                     : " unchecked!"),  
  35.                                     Toast.LENGTH_SHORT).show();  
  36.                         }  
  37.                     });  
  38.             return builder.create();  
  39.         }  
  40.         return null;  
  41.     } 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值