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();
    }
相关推荐
hnlgzb17 分钟前
常见的Android Jetpack库会有哪些?这些库中又有哪些常用类的?
android·android jetpack
钛态4 小时前
Flutter 三方库 http_mock_adapter — 赋能鸿蒙应用开发的高效率网络接口 Mock 与自动化测试注入引擎(适配鸿蒙 HarmonyOS Next ohos)
android·网络协议·flutter·http·华为·中间件·harmonyos
王码码20354 小时前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
左手厨刀右手茼蒿4 小时前
Flutter for OpenHarmony: Flutter 三方库 shamsi_date 助力鸿蒙应用精准适配波斯历法(中东出海必备)
android·flutter·ui·华为·自动化·harmonyos
代码飞天4 小时前
wireshark的高级使用
android·java·wireshark
2501_915918415 小时前
苹果App Store上架审核卡住原因分析与解决方案指南
android·ios·小程序·https·uni-app·iphone·webview
skiy5 小时前
MySQL Workbench菜单汉化为中文
android·数据库·mysql
小小小点6 小时前
Android四大常用布局详解与实战
android
MinQ7 小时前
binder和socket区别及原理
android
Ehtan_Zheng7 小时前
Jetpack Compose 中绘制发光边框的多种方式
android