当前位置: 移动技术网 > 移动技术>移动开发>Android > 【解决】AlertDialog和AlertDialog.Builder的区别是什么?

【解决】AlertDialog和AlertDialog.Builder的区别是什么?

2020年07月23日  | 移动技术网移动技术  | 我要评论

解决:

       查看AlertDialog源码:

protected AlertDialog(Context context) {   
    this(context, com.android.internal.R.style.Theme_Dialog_Alert);   
}   
  
protected AlertDialog(Context context, int theme) {   
    super(context, theme);   
    mAlert = new AlertController(context, this, getWindow());   
}   
  
protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {   
    super(context, com.android.internal.R.style.Theme_Dialog_Alert);   
    setCancelable(cancelable);   
    setOnCancelListener(cancelListener);   
    mAlert = new AlertController(context, this, getWindow());   
}

可以看出来它的构找函数是protected,那就不能直接通过构找函数去构建,那怎么构建AlertDialog对象呢?必须通过AlertDialog.Builder

       AlertDialog是Dialog的一个直接子类,AlertDialog也是Android系统当中最常用的对话框之一。

      一个AlertDialog可以有两个以上的Button,可以对一个AlertDialog设置相应的信息。比如title,massage,setSingleChoiceItems,setPositiveButton,setNegativeButton等等.

      但不能直接通过AlertDialog的构造函数来生产一个AlertDialog。只能通过Builder去构造:      

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

简单来说:AlertDialog.Builder是AlertDialog封装的一个内部类,实现了构造器模式,所有AlertDialog需要设置一些属性必须通过构造器去构造。Builder设计模式可以很好地控制参数的个数以及灵活的组合多种参数。

 

 

最后:

你说解决Android问题最需要什么?是把问题发给我,不用问在吗,客套一番,直接贴代码。我的联络方式,备注“解决问题”四个大字,别怕,我们不去爬山哈,也不收费,哈哈。

在这里插入图片描述

本文地址:https://blog.csdn.net/yxf0806/article/details/107407520

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网