Android中对话框之一:对话框基础

来源:互联网 发布:uboot 源码 编辑:程序博客网 时间:2024/06/15 18:10

1.继承关系



2.AlertDialog的创建方法


由于AlertDialog类的构造方法被声明成protected方法,因此,不能直接使用new关键字来创建AlertDialog类的对象实例,只能用AlertDialogBuilder.showAlertDialogBuilder.create+AlertDialog.show方法显示对话框,或者使用activity的

showDialogint id方法创建并显示对话框
使用show方法显示对话框是异步的。也就是说,当调用AlertDialogBuilder.showAlertDialog.show方法显示对话框后,show方法会立即返回,并且继续执行后面的代码。
AlertDialogBuilder.show实际上也是调用AlertDialog.show方法
public AlertDialog show() { 
    AlertDialog dialog=create(); 
    dialog.show(); 
    return dialog; 
AlertDialogBuilder.show方式创建并显示对话框:new AlertDialog.Builder(this).setTitle()

.setPositiveButton()

.setNeutralButton(…, …)

.setNegativeButton().show()

Builder.create+AlertDialog.show方式创建并显示对话框:
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle()

.setPositiveButton()

.setNeutralButton(…, …)

.setNegativeButton().create();

dialog.show();

showDialogint id方法创建并显示对话框:
首先在某个函数中(比如点击响应函数)中调用showDialog:
button01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(dialogid);
}
});
activity会回调onCreateDialog:
      @Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch(id){
case xxx:
      buildDiagxxx(MainActivity.this);
}
return null;
}
buildDiagxxx(Context context)
{     
new AlertDialog.Builder(MainActivity.this).setTitle()

.setPositiveButton()

.setNeutralButton(…, …)

.setNegativeButton().create();

}



3.AlertDialog扩展

有的时候,可能需要弹出一个对话框,以便从用户的输入来获取某些确认信息。这种情况下,可以重写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”按钮会使对话框消失。



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

[java] view plaincopy
  1. @Override  
  2. protected Dialog onCreateDialog(int id) {  
  3.     // ...  
  4. }  
当调用showDialog()的时候,上面被重写的方法就被调用了:
[java] view plaincopy
  1. public void onClick(View v) {  
  2.     showDialog(0);  
  3. }  
这个创建对话框的onCreateDialog()方法是一个被Activity控制的回调函数,当调用showDialog()时,onCreateDialog()回调函数就被触发了。showDialog()方法接受一个Integer参数,用来识别到底要显示哪个对话框。一般情况下,使用switch语句去判断显示不同的对话框。

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

[java] view plaincopy
  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.     } 



原创粉丝点击