Android系统封装了AlertDialog,用来给我们使用。我们可以通过
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("message")
.create()
.show();
这个其实就是典型的Builder设计模式,通过封装复杂的dialog对象,将组建和构建分离,当用户使用的时候可以直接调用组建,并最后创建出Dialog对象。
通过Dialog源码的分析,我们能够更好的了解Builder设计模式。
public class AlertDialog extends Dialog implements DialogInterface {
private AlertController mAlert;
AlertDialog(Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
super(context, createContextThemeWrapper ? resolveDialogTheme(context, themeResId) : 0,
createContextThemeWrapper);
mWindow.alwaysReadCloseOnTouchAttr();
mAlert = new AlertController(getContext(), this, getWindow());
}
@Override
public void setTitle(CharSequence title) {
super.setTitle(title);
mAlert.setTitle(title);
}
public static class Builder {
public Builder(Context context, int themeResId) {
P = new AlertController.AlertParams(new ContextThemeWrapper(
context, resolveDialogTheme(context, themeResId)));
}
public Builder setTitle(@StringRes int titleId) {
P.mTitle = P.mContext.getText(titleId);
return this;
}
public AlertDialog create() {
// Context has already been wrapped with the appropriate theme.
final AlertDialog dialog = new AlertDialog(P.mContext, 0, false);
P.apply(dialog.mAlert);
dialog.setCancelable(P.mCancelable);
if (P.mCancelable) {
dialog.setCanceledOnTouchOutside(true);
}
dialog.setOnCancelListener(P.mOnCancelListener);
dialog.setOnDismissListener(P.mOnDismissListener);
if (P.mOnKeyListener != null) {
dialog.setOnKeyListener(P.mOnKeyListener);
}
return dialog;
}
public AlertDialog show() {
final AlertDialog dialog = create();
dialog.show();
return dialog;
}
}
}
通过上面的代码我们可以看到,创建AlertDialog.Build的时候会创建一个AlertController.AlertParams对象,这个对象里面封装了所有的Dialog的属性,并且在调用Builder的类似于setTitle方法的时候会将参数赋值给AlertParams,当所有的组件的属性赋值好之后,就调用create()方法,这个方法里面就创建出AlertDialog对象,并调用P.apply(dialog.mAlert)方法,将AlertDialog构造函数创建出来的AlertController对象传递给P:
public void apply(AlertController dialog) {
if (mCustomTitleView != null) {
dialog.setCustomTitle(mCustomTitleView);
} else {
if (mTitle != null) {
dialog.setTitle(mTitle);
}
if (mIcon != null) {
dialog.setIcon(mIcon);
}
if (mIconId != 0) {
dialog.setIcon(mIconId);
}
if (mIconAttrId != 0) {
dialog.setIcon(dialog.getIconAttributeResId(mIconAttrId));
}
}
// 代码省略
}
在apply方法中,可以发现只是把AlertParams中的参数设置到AlertController中,当我们调用create方法的时候,就是讲AlertDialog对象的组件组装起来,当我调用show的时候就会调用dialog的show方法:
public void show() {
// 如果已经在显示状态,return
if (mShowing) {
if (mDecor != null) {
if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {
mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR);
}
mDecor.setVisibility(View.VISIBLE);
}
return;
}
mCanceled = false;
if (!mCreated) {
dispatchOnCreate(null);
}
onStart();
// 获取DecorView
mDecor = mWindow.getDecorView();
// 获取布局参数
WindowManager.LayoutParams l = mWindow.getAttributes();
if ((l.softInputMode
& WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) {
WindowManager.LayoutParams nl = new WindowManager.LayoutParams();
nl.copyFrom(l);
nl.softInputMode |=
WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
l = nl;
}
// 将decorView添加到WindowManager中
mWindowManager.addView(mDecor, l);
mShowing = true;
// 发送一个现实Dialog的消息
sendShowMessage();
}
在show方法中:
-
通过diapatchOnCreate方法,调用Dialog的onCreate方法,并最终调用installContent方法。
public void installContent() { /* We use a custom title so never request a window title */ mWindow.requestFeature(Window.FEATURE_NO_TITLE); // 设置窗口的视图 int contentView = selectContentView(); mWindow.setContentView(contentView); setupView(); setupDecor(); } private int selectContentView() { if (mButtonPanelSideLayout == 0) { return mAlertDialogLayout; } if (mButtonPanelLayoutHint == AlertDialog.LAYOUT_HINT_SIDE) { return mButtonPanelSideLayout; } // TODO: use layout hint side for long messages/lists return mAlertDialogLayout; }
而我们可以看到在调用selectContentView的时候,会去获取mButtonPanelSideLayout视图,并通过WindowManager的setContentView方法来将视图加载。
public AlertController(Context context, DialogInterface di, Window window) { final TypedArray a = context.obtainStyledAttributes(null, R.styleable.AlertDialog, R.attr.alertDialogStyle, 0); // 获取视图 mAlertDialogLayout = a.getResourceId( R.styleable.AlertDialog_layout, R.layout.alert_dialog); a.recycle(); }
再通过setupView方法初始化AlertDialog布局中的各个部分
调用AlertDialog的onStart方法
最后将DecorView添加到WindowManager中