建造者模式
创建性模式。简化复杂对象的创建过程,将对象的创造和内部实现解耦,分离部件和组装过程,使得构建过程和部件可自由扩展。
AlertDialog简单使用
AlertDialog.Builder(this)
.setTitle("title")//返回builder对象
.setMessage("message")//返回builder对象
.setOnCancelListener { }
.setPositiveButton("Positive"){dialogInterface,which ->
dialogInterface.dismiss()}
.setNegativeButton("Negative"){dialogInterface,_ ->
dialogInterface.dismiss()}//返回builder对象
.show()//构建AlertDialog并显示
AlterDialog解析
AlterDialog.pngAlertController.png
1. AlertDialog.Builder(this)
public static class Builder{
private final AlertParams P;//存储构建过程中传入的参数(title、点击事件等)
private final int mTheme;
public Builder(@NonNull Context context) {
this(context, AlertDialog.resolveDialogTheme(context, 0));
}
public Builder(@NonNull Context context, @StyleRes int themeResId) {
this.P = new AlertParams(new ContextThemeWrapper(context, AlertDialog.resolveDialogTheme(context, themeResId)));
this.mTheme = themeResId;
}
...
public AlertDialog.Builder setTitle(@Nullable CharSequence title) {
this.P.mTitle = title;
return this;
}
...
public AlertDialog.Builder setOnCancelListener(OnCancelListener onCancelListener) {
this.P.mOnCancelListener = onCancelListener;
return this;
}
...
}
在Builder没有调用create之前:
所有的赋值操作(Builder.setXxx())都会返回Builder本身,这也使得Builder可以链式调用
Builder.setXxx()中将传入参数存储在AlertParams中,在之后创建AlertDialog时给mAlert赋值
2. AlertDialog.Builder.create()
AlertDialog.class
protected AlertDialog(@NonNull Context context, @StyleRes int themeResId) {
super(context, resolveDialogTheme(context, themeResId));
this.mAlert = new AlertController(this.getContext(), this, this.getWindow());
}
public AlertDialog create() {
AlertDialog dialog = new AlertDialog(this.P.mContext, this.mTheme);
this.P.apply(dialog.mAlert);//将mAlert传入AlertParams,将之前AlertParams中存储的参数赋值给mAlert
dialog.setCancelable(this.P.mCancelable);
if (this.P.mCancelable) {
dialog.setCanceledOnTouchOutside(true);
}
dialog.setOnCancelListener(this.P.mOnCancelListener);
dialog.setOnDismissListener(this.P.mOnDismissListener);
if (this.P.mOnKeyListener != null) {
dialog.setOnKeyListener(this.P.mOnKeyListener);
}
return dialog;
}
public AlertDialog show() {
AlertDialog dialog = this.create();
dialog.show();
return dialog;
}
AlertController.class
public static class AlertParams {
...
public void apply(AlertController dialog) {
if (this.mCustomTitleView != null) {
dialog.setCustomTitle(this.mCustomTitleView);
} else {
if (this.mTitle != null) {
dialog.setTitle(this.mTitle);
}
if (this.mIcon != null) {
dialog.setIcon(this.mIcon);
}
if (this.mIconId != 0) {
dialog.setIcon(this.mIconId);
}
if (this.mIconAttrId != 0) {
dialog.setIcon(dialog.getIconAttributeResId(this.mIconAttrId));
}
}
if (this.mMessage != null) {
dialog.setMessage(this.mMessage);
}
if (this.mPositiveButtonText != null || this.mPositiveButtonIcon != null) {
dialog.setButton(-1, this.mPositiveButtonText, this.mPositiveButtonListener, (Message)null, this.mPositiveButtonIcon);
}
if (this.mNegativeButtonText != null || this.mNegativeButtonIcon != null) {
dialog.setButton(-2, this.mNegativeButtonText, this.mNegativeButtonListener, (Message)null, this.mNegativeButtonIcon);
}
if (this.mNeutralButtonText != null || this.mNeutralButtonIcon != null) {
dialog.setButton(-3, this.mNeutralButtonText, this.mNeutralButtonListener, (Message)null, this.mNeutralButtonIcon);
}
if (this.mItems != null || this.mCursor != null || this.mAdapter != null) {
this.createListView(dialog);
}
if (this.mView != null) {
if (this.mViewSpacingSpecified) {
dialog.setView(this.mView, this.mViewSpacingLeft, this.mViewSpacingTop, this.mViewSpacingRight, this.mViewSpacingBottom);
} else {
dialog.setView(this.mView);
}
} else if (this.mViewLayoutResId != 0) {
dialog.setView(this.mViewLayoutResId);
}
}
AlertParams中保存创建过程中的参数
AlertController中通过AlertParams的参数给AlertDialog设置值和点击事件等
网友评论