Android 自定义builder对话框

一、对话框代码


/**
 * 对话框
 */
public class IntroduceDialog extends Dialog {

    private Context context;
    private int height, width;
    private boolean cancelTouchout;
    private View view;

    private IntroduceDialog(Builder builder) {
        super(builder.context);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }


    private IntroduceDialog(Builder builder, int resStyle) {
        super(builder.context, resStyle);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }

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

        setContentView(view);

        setCanceledOnTouchOutside(cancelTouchout);

        Window win = getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.height = height;
        lp.width = width;
        win.setAttributes(lp);
    }

    /**
     * Builder类
     */
    public static final class Builder {

        private Context context;
        private int height, width;
        private boolean cancelTouchout;
        private View view;
        private int resStyle = -1;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder view(int resView) {
            view = LayoutInflater.from(context).inflate(resView, null);
            return this;
        }

        public Builder heightpx(int val) {
            height = val;
            return this;
        }

        public Builder widthpx(int val) {
            width = val;
            return this;
        }

        public Builder heightdp(int val) {
            height = DipPx.dip2px(context, val);
            return this;
        }

        public Builder widthdp(int val) {
            width = DipPx.dip2px(context, val);
            return this;
        }

        public Builder heightDimenRes(int dimenRes) {
            height = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder widthDimenRes(int dimenRes) {
            width = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder style(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder cancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        public Builder addViewOnclick(int viewRes, View.OnClickListener listener) {
            view.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }


        public IntroduceDialog build() {
            if (resStyle != -1) {
                return new IntroduceDialog(this, resStyle);
            } else {
                return new IntroduceDialog(this);
            }
        }
    }

}

二、样式

    
    

三、使用

   /**
     * 显示
     * 对话框
     */
    private IntroduceDialog dialog;

    private void showDialog() {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.back:
                        if (dialog != null) {
                            dialog.cancel();
                        }
                        break;
                    case R.id.tv_play:

                        break;
                    case R.id.tv_sure:
                        mViewModel.scanblutooth();
//                        Toast.makeText(aty, "点击确定按钮", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };

        IntroduceDialog.Builder builder = new IntroduceDialog.Builder(this);

        dialog = builder
                .style(R.style.Dialog)
                .heightDimenRes(R.dimen.dp_400)
                .widthDimenRes(R.dimen.dp_640)
                .cancelTouchout(false)
                .view(R.layout.oxy_opr_tips)
                .addViewOnclick(R.id.back, listener)
                .addViewOnclick(R.id.tv_play, listener)
                .addViewOnclick(R.id.tv_sure, listener)
                .build();
        dialog.show();
    }

你可能感兴趣的:(移动端,Android,android)