Android 写一个自己的构建者模式的dialog

Android 写一个自己的构建者模式的dialog

在平时的工作中有很多的时候都会遇到提示的弹窗,虽然有系统的AlertDialog,但是还是有很多不方便,然后就自己写了一个提示类型的dialog

先上个图看看我的弹窗的效果

带标题栏的的

不带标题栏的
在这里插入图片描述
不带标题栏的只有一个按钮的
在这里插入图片描述

接下来我们看看这个dialog的写法
啥也不说我们先整一个布局 布局的代码就不整出来了,整个图就行了我给一个一会文章末尾会给出源码
在这里插入图片描述
接下来就来看看我们的构建者模式,在写之前我还是看了一下AlertDialog的源码。
既然我们要写dialog 直接写一个类继承dialog
算了直接上代码

public class BfMessageDialog extends Dialog implements View.OnClickListener {
    private String titleText;
    private int titleSzie;
    private int titleColor;
    private String message;
    private String leftBtnText;
    private String rightBtnText;
    private boolean isCancel;
    private int messageColor;
    private int messageSize;
    private onClickListener listener;
    private boolean isHaveTitle = false;
    private LinearLayout backgroundLayout;

    private boolean isOneBtn = false;
    private int LeftButtonContentSize;
    private int LeftButtonContentColor;
    private int RightButtonContentSize;
    private int RightButtonContentColor;

    private int BackGroundRes;
    private int lineColor;


    //控件
    private LinearLayout title_layout;
    private TextView title, content, left_btn, right_btn;
    private View divider1, divider2,divider3;

    @Override
    protected void onStart() {
        super.onStart();
        this.getWindow().setDimAmount(0f);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

    }


    private void initView() {
        setContentView(R.layout.chat_room_all_dialog);
        setCanceledOnTouchOutside(true);


        this.setCancelable(isCancel);

        title_layout = findViewById(R.id.title_layout);
        title = findViewById(R.id.title);
        content = findViewById(R.id.content);
        left_btn = findViewById(R.id.left_btn);
        right_btn = findViewById(R.id.right_btn);
        divider1 = findViewById(R.id.divider1);
        divider2 = findViewById(R.id.divider2);
        divider3 = findViewById(R.id.divider3);
        backgroundLayout = findViewById(R.id.backgroundLayout);

        //是否有标题栏
        if (isHaveTitle) {
            //设置标题栏
            if (!JudgeNullUtil.isNull(titleText)) {
                title_layout.setVisibility(View.VISIBLE);
                title.setText(titleText);
            } else {
                title_layout.setVisibility(View.GONE);
            }
            //设置标题栏的颜色和字体大小
            if (titleColor != 0) {
                title.setTextColor(titleColor);
            }
            if (titleSzie != 0) {
                title.setTextSize(titleSzie);
            }
        } else {
            title_layout.setVisibility(View.GONE);
        }


        //设置信息
        if (!JudgeNullUtil.isNull(message)) {
            content.setText(message);
            content.setVisibility(View.VISIBLE);

            //设置信息的颜色
            if (messageColor != 0) {
                content.setTextColor(messageColor);
            }

            //设置信息的字体大小
            if (messageSize != 0) {
                content.setTextSize(messageSize);
            }

        } else {
            content.setVisibility(View.GONE);
        }


        //是否只有一个按钮
        if (isOneBtn) {
            divider2.setVisibility(View.GONE);
            left_btn.setVisibility(View.VISIBLE);
            right_btn.setVisibility(View.GONE);
        } else {
            divider2.setVisibility(View.VISIBLE);
            left_btn.setVisibility(View.VISIBLE);
            right_btn.setVisibility(View.VISIBLE);
        }

        //设置左边按钮上面的文字
        if (!JudgeNullUtil.isNull(leftBtnText)) {
            //左边文字的
            left_btn.setText(leftBtnText);

            //左边文字大小
            if (LeftButtonContentSize != 0) {
                left_btn.setTextSize(LeftButtonContentSize);
            }

            //左边文字颜色
            if (LeftButtonContentColor != 0) {
                left_btn.setTextColor(LeftButtonContentColor);
            }
        }

        //设置右边按钮上面的文字
        if (!JudgeNullUtil.isNull(rightBtnText)) {
            //右边文字
            right_btn.setText(rightBtnText);

            //右边的文字大小
            if (RightButtonContentSize != 0) {
                right_btn.setTextSize(RightButtonContentSize);
            }

            //右边的文字颜色
            if (RightButtonContentColor != 0) {
                right_btn.setTextColor(RightButtonContentColor);
            }
        }

        //设置背景颜色
        if (BackGroundRes != 0) {
            backgroundLayout.setBackgroundResource(BackGroundRes);
        }

        //设置线的颜色
        if (lineColor != 0) {
            divider1.setBackgroundColor(lineColor);
            divider2.setBackgroundColor(lineColor);
            divider3.setBackgroundColor(lineColor);
        }


        left_btn.setOnClickListener(this);
        right_btn.setOnClickListener(this);

        Window window = this.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = WindowManager.LayoutParams.WRAP_CONTENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(attributes);
        window.setWindowAnimations(R.style.DialogOutAndInStyle);
        window.setGravity(Gravity.CENTER);

    }


    private BfMessageDialog(Builder builder) {
        super(builder.context, R.style.mydialog);
        this.titleText = builder.title;
        this.titleSzie = builder.titleSize;
        this.titleColor = builder.titleColor;
        this.message = builder.message;
        this.listener = builder.listener;
        this.isCancel = builder.isCancel;
        this.leftBtnText = builder.LeftButtonContent;
        this.rightBtnText = builder.RightButtonContent;
        this.messageColor = builder.messageColor;
        this.messageSize = builder.messageSize;
        this.isHaveTitle = builder.isHaveTitle;

        this.isOneBtn = builder.isOneBtn;
        this.LeftButtonContentSize = builder.LeftButtonContentSize;
        this.LeftButtonContentColor = builder.LeftButtonContentColor;
        this.RightButtonContentSize = builder.RightButtonContentSize;
        this.RightButtonContentColor = builder.RightButtonContentColor;

        this.BackGroundRes = builder.BackGroundRes;
        this.lineColor = builder.lineColor;

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.left_btn:
                if (listener != null) {
                    listener.onLeftClick(this);
                }
                break;
            case R.id.right_btn:
                if (listener != null) {
                    listener.onRightClick(this);
                }
                break;
        }
    }

    public interface onClickListener {
        void onLeftClick(Dialog dialog);

        void onRightClick(Dialog dialog);
    }


    public static class Builder {
        private Context context;

        private String title;
        private int titleColor;
        private int titleSize;
        private boolean isHaveTitle;

        private String message;
        private int messageColor;
        private int messageSize;

        private boolean isCancel;
        private boolean isOneBtn;

        private String LeftButtonContent;
        private int LeftButtonContentSize;
        private int LeftButtonContentColor;


        private String RightButtonContent;
        private int RightButtonContentSize;
        private int RightButtonContentColor;

        private int BackGroundRes;
        private int lineColor;

        private onClickListener listener;


        /**
         * 建造器的构造方法:
         *
         * @param context
         */
        public Builder(Context context) {
            this.context = context;
        }


        public Builder setHaveTitle(boolean b) {
            this.isHaveTitle = b;
            return this;
        }


        /**
         * @param title 标题文字
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * @param color 标题的文字颜色
         * @param size  标题的文字大小
         * @return
         */
        public Builder setTitleColorAndroidSize(int color, int size) {
            this.titleColor = color;
            this.titleSize = size;
            return this;
        }

        /**
         * @param message 设置提示的文字
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }


        /**
         * @param color 提示文字的文字颜色
         * @param size  提示文字的文字大小
         * @return
         */
        public Builder setMessageColorAndroidSize(int color, int size) {
            this.messageColor = color;
            this.messageSize = size;
            return this;
        }


        /***
         *
         * @param isCancel  设置外部是否点击可关闭
         * @return
         */
        public Builder setIsCancle(boolean isCancel) {
            this.isCancel = isCancel;
            return this;
        }


        /**
         * @param isOneBtn 是否只有一个按钮  只有一个按钮用左边
         * @return
         */
        public Builder setIsOneBtn(boolean isOneBtn) {
            this.isOneBtn = isOneBtn;
            return this;
        }


        /**
         * @param text 左边按钮的文字
         * @return
         */
        public Builder setLeftButton(String text) {
            this.LeftButtonContent = text;
            return this;
        }

        /**
         * @param color 左边按钮的颜色
         * @param size  左边按钮的大小
         * @return
         */
        public Builder setLeftButtonColorAndSize(int color, int size) {
            this.LeftButtonContentColor = color;
            this.LeftButtonContentSize = size;
            return this;
        }

        /**
         * @param text 右边按钮的文字
         * @return
         */
        public Builder setRightButton(String text) {
            this.RightButtonContent = text;
            return this;
        }


        /**
         * @param color 右边按钮的颜色
         * @param size  右边按钮的大小
         * @return
         */
        public Builder setRightButtonColorAndSize(int color, int size) {
            this.RightButtonContentColor = color;
            this.RightButtonContentSize = size;
            return this;
        }


        /**
         * @param Res 设置背景文件
         * @return
         */
        public Builder setBackGroundRes(int Res) {
            this.BackGroundRes = Res;
            return this;
        }

        /**
         * @param color 线的颜色
         * @return
         */
        public Builder setLineColor(int color) {
            this.lineColor = color;
            return this;
        }


        /**
         * @param lisener 按钮的点击事件
         * @return
         */
        public Builder setOnClickLisener(onClickListener lisener) {
            this.listener = lisener;
            return this;
        }

        public BfMessageDialog build() {
            return new BfMessageDialog(this);
        }

    }
}

然后看看用法

BfMessageDialog.Builder builder = new BfMessageDialog.Builder(context);
                builder.setHaveTitle(false)   //是否要标题
                        .setIsOneBtn(true)   //是否只有一个按钮  只有一个按钮的情况下面只有设置左边的按钮
                        .setIsCancle(true)   //外部点击是否可以关闭
                        .setTitle("提示框")    //标题栏的文字
                        .setTitleColorAndroidSize(0xFF3F51B5, 18)   //标题栏的文字大小  文字颜色
                        .setMessage("欢迎来到洒家卖蘑菇博客")                      //中间信息部分
                        .setMessageColorAndroidSize(0xFF009688, 16)   //中间信息部分的文字  大小  颜色
                        .setLeftButton("取消")                                   //左边按钮的文字
                        .setLeftButtonColorAndSize(0xFFFF5722, 16)      //左边文字的  颜色  大小
                        .setRightButton("确定")                                   //右边文字
                        .setRightButtonColorAndSize(0xFF47DDDB, 16)     //右边文字的颜色大小
                        .setBackGroundRes(R.drawable.chat_room_all_layout)        // dialog 的背景文件
                        .setLineColor(0xffececec)                                  //线的颜色
                        .setOnClickLisener(new BfMessageDialog.onClickListener() {    //监听事件
                            @Override
                            public void onLeftClick(Dialog dialog) {
                                dialog.dismiss();
                                MyToast.addToast(context,"取消");
                            }

                            @Override
                            public void onRightClick(Dialog dialog) {
                                dialog.dismiss();
                                MyToast.addToast(context,"确定");
                            }
                        }).build().show();

搞定以后项目里面再也不用写那么多个dialog了。
给一个下载的链接
dialog 和dialog的布局下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值