`
qcyycom
  • 浏览: 181993 次
社区版块
存档分类
最新评论

Android 程式开发:(一)详解Activity —— 1.4显示“普通”对话框

阅读更多

 

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

    1.创建一个名为Dialog的工程。

    2.main.xml中的代码。

 

[java] 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. <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 plaincopy
  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”按钮会使对话框消失。

效果图:

更多信息请查看 java进阶网 http://www.javady.com

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics