分享

AlertDialog和AlertDialog.Builder?两者区别

 tracyf 2013-12-25

 那我们先了解什么是AlertDialog?什么是AlertDialog.Builder?且两者有什么区别?

   AlertDialog是Dialog的一个直接子类,AlertDialog也是Android系统当中最常用的对话框之一。
   一个AlertDialog可以有两个以上的Button,可以对一个AlertDialog设置相应的信息。比如title,massage,setSingleChoiceItems,setPositiveButton,setNegativeButton等等。。。。

   但不能直接通过AlertDialog的构造函数来生产一个AlertDialog。研究AlertDialog的源码发现AlertDialog所有的构造方法都是写保护的所以不能通过:AlertDialog alertDialog  = new AlertDialog();来得到。AlertDialog构造方法源码如下:

  1. protected AlertDialog(Context context) {     
  2.     this(context, com.android.internal.R.style.Theme_Dialog_Alert);     
  3. }     
  4.     
  5. protected AlertDialog(Context context, int theme) {     
  6.     super(context, theme);     
  7.     mAlert = new AlertController(context, this, getWindow());     
  8. }     
  9.     
  10. protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {     
  11.     super(context, com.android.internal.R.style.Theme_Dialog_Alert);     
  12.     setCancelable(cancelable);     
  13.     setOnCancelListener(cancelListener);     
  14.     mAlert = new AlertController(context, this, getWindow());     
  15. }    


只能通过:

  1. AlertDialog.Builder alertDialog  =new  AlertDialog.Builder(this);    

来得到。

那就通过一个具体的实例来说说吧(这里用一个最常用的对话框为例):

  1. package com.oyah;     
  2.     
  3. import android.app.Activity;     
  4. import android.app.AlertDialog;     
  5. import android.content.DialogInterface;     
  6. import android.os.Bundle;     
  7.     
  8. public class TestsActivity extends Activity {     
  9.     public void onCreate(Bundle savedInstanceState) {     
  10.         super.onCreate(savedInstanceState);     
  11.         setContentView(R.layout.main);     
  12.     
  13.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);     
  14.         alertDialog     
  15.                 .setTitle("title")     
  16.                 .setMessage("message")     
  17.                 .setPositiveButton("okBuuon",     
  18.                         new DialogInterface.OnClickListener() {     
  19.     
  20.                             @Override    
  21.                             public void onClick(DialogInterface dialog,     
  22.                                     int which) {     
  23.     
  24.                             }     
  25.                         })     
  26.                 .setNegativeButton("exitButton",     
  27.                         new DialogInterface.OnClickListener() {     
  28.     
  29.                             @Override    
  30.                             public void onClick(DialogInterface dialog,     
  31.                                     int which) {     
  32.     
  33.                             }     
  34.                         }).[b]setCancelable(false).[/b]create().show();     
  35.     }     
  36. }    


针对AlertDialog中设置了确定和取消按钮,一般来说确定为执行某个动作,取消就是不执行,但是如果用户点击了系统的Back键,此时就会将AlertDialog关闭,而并没有执行预期的取消的操作。

此时需要关注一个方法setCancelable(false) 该方法定义设置该AlertDialog是否可以被Back键取消,如果不设置默认为true
下面是一些扩展:
根据AlertDialog.Builder 创建 相应的 AlertDialog

  1. AlertDialog alertDialogs = alertDialog.create();    


用dismiss();方法来清除创建过的AlertDialog

  1. alertDialogs.dismiss();    


以上所采用的都是AlertDialog 系统默认的布局 现在说自定义布局的情况 并添加一个用于取消AlertDialog 的 Button

定义其布局 main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas./apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:padding="10dp"  
  7.     >  
  8. <LinearLayout   
  9.     android:orientation="vertical"  
  10.     android:layout_width="wrap_content"  
  11.     android:layout_height="wrap_content"  
  12.     >  
  13.  </LinearLayout>  
  14. </LinearLayout>  


通过LayoutInflater 得到上面 main.xml 布局文件

  1. view = this.getLayoutInflater().inflate(R.layout.main, null);  


指定AlertDialog.Builder 所需的布局 并返回目标AlertDialog

  1. alertDialog.setView(view);  
  2.   
  3. alertDialogs= alertDialog.create();  


通过 view.findViewById() 得到 目标View 然后设置其内容 如:

  1. TextView title = (TextView) view.findViewById(R.id.title);  
  2. title.setTextSize(20);  
  3. title.setTextColor(Color.RED);  
  4. title.setText("Title");  
  5.           
  6. TextView message = (TextView) view.findViewById(R.id.message);  
  7. message.setText("message");  


显示对话框 AlertDialog

  1. findViewById(R.id.button).setOnClickListener(new OnClickListener(){  
  2.             public void onClick(View v) {  
  3.                 // TODO Auto-generated method stub  
  4.                 alertDialogs.show();  
  5.             }  
  6.         });  


清除对话框 AlertDialog

  1. view.setOnClickListener(new OnClickListener(){  
  2.             public void onClick(View v) {  
  3.                 // TODO Auto-generated method stub  
  4.                 alertDialogs.dismiss();  
  5.             }  
  6.         });  



 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多