【转载】出处:http://www.cnblogs.com/shaweng/p/3800238.html
打开源码,首先映入眼帘的是三个构造方法,但这三个构造方法都是protected类型的,不允许我们直接实例化AlertDialog.。因此,我们再看别的有没有方法.可以实例化 。
再仔细一看,发现一个变量 :AlertController mAlert, 这个才是我们今天的主角,重点研究它.
mAlert的定义在 ,以上我们提到的AlertDialog的构造函数,
AlertDialog(Context context, int theme, boolean createThemeContextWrapper) {
super(context, resolveDialogTheme(context, theme), createThemeContextWrapper);
mWindow.alwaysReadCloseOnTouchAttr();
mAlert = new AlertController(getContext(), this, getWindow());
}
此外,我们还发现,AlertDialog中几乎所有的方法都是通过这个mAlert变量来操作的。
也就是说,AlertDialog是一个空壳,并没有实际的作用, 它的实质是AlertController类. 就是我刚才说的 “主角” 。 而Alertcontroller是android内部类,在package com.android.internal.app包中,不能在Eclipse中通过ctrl键来跟踪源码,可以使用Source Insight软件打开该软件源码查看。
再往下看,AlertDialog 类中,发现一个静态类 Builder, 通过这个类,我们也可以直接创建一个Dialog,。
Builder静态类中也同样有一个AlertController.Alertparams的类的对象 p,我们再看Builder的源码,它里面可以设置所有Dialog的的方法,包括
setTitle(CharSequence title)
setMessage(int messageId);
setIcon(int iconId);
setPositiveButton()
等等,我们看这些方法的的内部实现,全部是针对它的内置对象P 来实现的, 也就是我们所调用的这些方法,最终结果实际上是用在了AlertController.AlertParams 类的对象p 身上。
别外还有一个有趣的现象,这些方法的返回值都是Builder对象, 也就是说,我们可以连级调用此方法,。
而显示一个Dialog,仅仅调用这些set 方法,还不够,最后还差一步, 就是调用create()方法,这个方法是最后调用的,也就是创建对话框, 我们看,create方法中都做了那些动作
public AlertDialog create() {
final AlertDialog dialog =new AlertDialog(p.mContext);
p.apply(dialog.mAlert)
dialog.setCancelable(P.mCancelable);
return dialog;
}
这个是create()的实现,我们可以很直观地看出是它帮我们创建了一个AlertDialog对象。
这里的p, 就是我们创建的AlertController.Alertparams的对象,
而在AlertDialog dialog=new AlertDialog(p.mContext); 这句代码中,定义Dialog的同时也定义了 一个AlertController 的对象 mAlert;。
在第二句代码中,p.apply(dialog.mAlert) ,我们跟踪过去,看下,哈哈,原来是,我们刚才调用的set方法的值,又还原给新定义的dialog中的 mAlert对象了, 这下明白了,原来 P只不过是一个 “屁” ,暂时的存放数据的对象,只要我们调用create方法,P就会乖乖的把数据交给dialog对象,
除了create方法以外, 还有一个show() 方法,实现如下
public AlertDialog show() {
AlertDialog dialog=create();
dialog.show();
return dialog;
}
我们可以看出,show() 也是调用的 create方法.
由以下的分析,我们就可能轻松理解,如下我们常用的语句了,
return new AlertDialog.Builder(context).
setIcon(R.drawable.alert_icon).
setTitle(R.string.alert_str)
.create() ;