AlertDialog和AlertDialog.Builder两者区别

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

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

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

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


只能通过:

[java] view plain copy
  1. AlertDialog.Builder alertDialog  =new  AlertDialog.Builder(this);    

来得到。

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

[java] view plain copy
  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

[java] view plain copy
  1. AlertDialog alertDialogs = alertDialog.create();    


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

[java] view plain copy
  1. alertDialogs.dismiss();    


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

定义其布局 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: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 布局文件

[java] view plain copy
  1. view = this.getLayoutInflater().inflate(R.layout.main, null);  


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

[java] view plain copy
  1. alertDialog.setView(view);  
  2.   
  3. alertDialogs= alertDialog.create();  


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

[java] view plain copy
  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

[java] view plain copy
  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

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



 

                            </td>
                            <script>
                                var html = document.getElementById("artContent").innerHTML;
                                document.getElementById("artContent").innerHTML = html;

                            </script>
                        </tr>
                    </tbody>
                </table>
                <div id="viewerPlaceHolder" style="width: 717px; height: 700px; display: none; margin: 0 auto;">
                </div>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值