最近看一些android的代码,发现他的AlertDialog是需要用类似下面的代码来创建的:
进入到AlertDialog的源代码,可以看到AlertDialog是继承于Dialog,并且他的构造函数都是声明成protected的。而Builder是一个内部的public static class. Builder的大部分方法是在设置一个AlertController.AlertParams 的object.只有在调用Builder.create或者show的时候(其实show也是先调用了create),才会真正的new一个AlertDialog的对象,并且把AlertParams的某些参数设置给这个AlertDialog对象。
但是我始终没想明白这样做会带来什么特别的效果或者说好处。只是为了结构上看起来稍微清晰一点?还是有深层次的含义呢?
结论:这是一个builder模式构建复杂对象的应用。在Builder.create中,或者在params的设置中,就能够实现对AlertDialog本身的约束规则。AlertDialog声明protected的构造函数就防止了外部代码不通过build而直接去new对象,造成约束失败的情况。
分享到:
相关推荐
android 使用alertdialog创建自定义对话框
常用的Builder对话框设置,包括只显示文本、带控件、自定义空、Builder间嵌套等
android AlertDialog使用例程 一个在安卓平台上开发使用AlertDialog的例子 很实用很全哦
Android中使用AlertDialog的例子,包括简单提示框、单选提示框、多选提示框、自定义提示框、Activity和提示框的转换。
Android之AlertDialog源码
android 显示 AlterDialog
1、从Activity的TextView中获取字符串设置到AlertDialog的TextView和EditText中 2、将AlertDialog的EditText中的值设置到Activity的TextView中 新手在自定义AlertDialog上的疑问笔者猜测主要有两个: 1、自定义的...
项目中为了实现账号多设备登录的监听 一个账号在别的设备登录时在该设备上需要弹出对话框提示...AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setIcon(R.drawable.logo_mini); builder.setTi
安卓Android源码——中实现Iphone样式的AlertDialog.zip
android AlertDialog 单选 多选 输入框 疯狂android讲义
android自定义AlertDialog,例如支付页面,先弹出自定义AlertDialog询问是否愿意支付,确定则弹出另外一个自定义AlertDialog,选择支付方式,android自定义AlertDialog,android自定义AlertDialog,android自定义...
AlertDialog.Builder多种不同用法。
在Android中实现Iphone样式的AlertDialog; 使用Dialog实现。
安卓Android源码——alertDialog.rar
android AlertDialog对话框的类型集合 ,比较全面的讲述了 AlertDialog 的 种类 , Demo 的综合性比较强 , Demo 可直接运行
【Android】对话框 AlertDialog
Android之修改AlertDialog对话框及使用系统Holo风格
Android alertDialog对话框.zip源码资源下载Android alertDialog对话框.zip源码资源下载
Android中实现Iphone样式的AlertDialog.rar
Android AlertDialog对话框 自定义实现遮罩层