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);
            }
        }
    }

}

二、样式

    <!-- 格式化 -->
    <style name="Dialog" parent="android:style/Theme.Dialog">
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
    </style>

三、使用

   /**
     * 显示
     * 对话框
     */
    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();
    }
相关推荐
编程洪同学1 小时前
Spring Boot 中实现自定义注解记录接口日志功能
android·java·spring boot·后端
氤氲息3 小时前
Android 底部tab,使用recycleview实现
android
Clockwiseee4 小时前
PHP之伪协议
android·开发语言·php
小林爱4 小时前
【Compose multiplatform教程08】【组件】Text组件
android·java·前端·ui·前端框架·kotlin·android studio
小何开发5 小时前
Android Studio 安装教程
android·ide·android studio
开发者阿伟5 小时前
Android Jetpack LiveData源码解析
android·android jetpack
weixin_438150995 小时前
广州大彩串口屏安卓/linux触摸屏四路CVBS输入实现同时显示!
android·单片机
CheungChunChiu6 小时前
Android10 rk3399 以太网接入流程分析
android·framework·以太网·eth·net·netd
木头没有瓜7 小时前
ruoyi 请求参数类型不匹配,参数[giftId]要求类型为:‘java.lang.Long‘,但输入值为:‘orderGiftUnionList
android·java·okhttp
键盘侠0077 小时前
springboot 上传图片 转存成webp
android·spring boot·okhttp