采用Builder设计模式,打造一个万能的dialog,使用时一句话调用不用再写一大串代码
框架图.png
一、搭建框架
创建五个对象类
1.AlertDialog ---- Builder创建
public class AlertDialog extends Dialog {
public AlertDialog(Context context, int themeResId) {
super(context, themeResId);
}
public static class Builder{
}
}
2.AlertController ----AlertParams创建
class AlertController {
public AlertController(AlertDialog alertDialog, Window window) {
}
public static class AlertParams{
}
}
3.DialogViewHelper 创建
public class DialogViewHelper {
public DialogViewHelper(Context context, int layoutResId) {
}
}
4.AlertDialog -----Builder
AlertDialog 构造:
private AlertController mAlert;
public AlertDialog(Context context, int themeResId) {
super(context, themeResId);
mAlert=new AlertController(this,getWindow());
}
Builder 构造:
private final AlertController.AlertParams P;
/**
* Creates a builder for an alert dialog that uses the default alert
* dialog theme.
*
* The default alert dialog theme is defined by
* {@link android.R.attr#alertDialogTheme} within the parent
* {@code context}'s theme.
*
* @param context the parent context
*/
public Builder(Context context) {
this(context,R.style.dialog);
}
/**
* Creates a builder for an alert dialog that uses an explicit theme
* resource.
*
* The specified theme resource ({@code themeResId}) is applied on top
* of the parent {@code context}'s theme. It may be specified as a
* style resource containing a fully-populated theme, such as
* {@link android.R.style#Theme_Material_Dialog}, to replace all
* attributes in the parent {@code context}'s theme including primary
* and accent colors.
*
* To preserve attributes such as primary and accent colors, the
* {@code themeResId} may instead be specified as an overlay theme such
* as {@link android.R.style#ThemeOverlay_Material_Dialog}. This will
* override only the window attributes necessary to style the alert
* window as a dialog.
*
* Alternatively, the {@code themeResId} may be specified as {@code 0}
* to use the parent {@code context}'s resolved value for
* {@link android.R.attr#alertDialogTheme}.
*
* @param context the parent context
* @param themeResId the resource ID of the theme against which to inflate
* this dialog, or {@code 0} to use the parent
* {@code co