《DialogFragment系列一之源码分析》
《DialogFragment系列二之Dialog封装》
《DialogFragment系列三之AlertDialog实现》
《DialogFragment系列四之StatusDialog(Progress、Success、Error)实现》
《DialogFragment系列五之ItemDialog(eg:BottomDialog)实现》
《DialogFragment系列六之常见问题》
上篇《DialogFragment系列二之Dialog封装》中基于DialogFragment封装了Dialog,可以初步用来替代原生Android Dialog,但是更具体功能还需要在Dialog的基础上发扬光大,可以将Dialog理解为BaseDialog,下面来定义一下AlertDialog,还是老办法,通过代码说明。
效果图:
代码实现:
public class AlertDialog extends Dialog {
private DialogParams mDialogParams;
private AlertDialog() {
mDialogParams = new DialogParams();
}
@Override
protected int setLayoutRes() {
return R.layout.dialog_alert;
}
public static Builder with(AppCompatActivity activity) {
return new Builder(activity);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
TextView tvDialogCommonTitle = view.findViewById(R.id.tv_dialog_common_title);
TextView tvDialogCommonContent = view.findViewById(R.id.tv_dialog_common_content);
TextView tvDialogCommonPositive = view.findViewById(R.id.tv_dialog_common_positive);
TextView tvDialogCommonNegative = view.findViewById(R.id.tv_dialog_common_negative);
//填充数据
tvDialogCommonTitle.setText(mDialogParams.title);
tvDialogCommonContent.setText(mDialogParams.content);
tvDialogCommonPositive.setText(mDialogParams.positive);
tvDialogCommonNegative.setText(mDialogParams.negative);
//监听
tvDialogCommonPositive.setOnClickListener(this);
tvDialogCommonNegative.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_dialog_common_positive:
dismiss();
if (mDialogParams.onPositiveClickListener != null) {
mDialogParams.onPositiveClickListener.onClick(v);
}
break;
case R.id.tv_dialog_common_negative:
dismiss();
if (mDialogParams.onNegativeClickListener != null) {
mDialogParams.onNegativeClickListener.onClick(v);
}
break;
}
}
@Override
protected boolean setCancelable() {
return mDialogParams.isCancelable;
}
public class DialogParams {
String title;
String positive;
String negative;
String content;
boolean isCancelable = true;
View.OnClickListener onPositiveClickListener;
View.OnClickListener onNegativeClickListener;
}
public static class Builder {
AppCompatActivity activity;
DialogParams P;
AlertDialog alertDialog;
public Builder(AppCompatActivity activity) {
alertDialog = new AlertDialog();
this.P = alertDialog.mDialogParams;
this.activity = activity;
}
public Builder setTitle(String val) {
P.title = val;
return this;
}
public Builder setContent(String val) {
P.content = val;
return this;
}
public Builder setCancelable(boolean val) {
P.isCancelable = val;
return this;
}
public Builder setPositiveButton(String positive, View.OnClickListener onClickListener) {
P.onPositiveClickListener = onClickListener;
P.positive = positive;
return this;
}
public Builder setNegativeButton(String negative, View.OnClickListener onClickListener) {
P.onNegativeClickListener = onClickListener;
P.negative = negative;
return this;
}
public Builder setPositiveButton(@StringRes int positive, View.OnClickListener onClickListener) {
P.onPositiveClickListener = onClickListener;
P.positive = activity.getString(positive);
return this;
}
public Builder setNegativeButton(@StringRes int negative, View.OnClickListener onClickListener) {
P.onNegativeClickListener = onClickListener;
P.negative = activity.getString(negative);
return this;
}
public AlertDialog show() {
alertDialog.show(activity);
return alertDialog;
}
}
}
AlertDialog继承自Dialog,重写了其setLayoutRes()用来加载布局,onViewCreated()初始化控件并添加数据,setCancelable()设置是否可以点击外部dismiss,onClick()来设置监听,AlertDialog同样采用构造者模式,实现了自己的DialogParams和Builder并提供给了对外接口static with()方法。
读者可扩展更多的属性,若有疑问或其他观点请留言交流!!!