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();
    }
相关推荐
萌面小侠Plus35 分钟前
Android笔记(三十三):封装设备性能级别判断工具——低端机还是高端机
android·性能优化·kotlin·工具类·低端机
慢慢成长的码农35 分钟前
Android Profiler 内存分析
android
大风起兮云飞扬丶36 分钟前
Android——多线程、线程通信、handler机制
android
L725642 分钟前
Android的Handler
android
清风徐来辽43 分钟前
Android HandlerThread 基础
android
HerayChen2 小时前
HbuildderX运行到手机或模拟器的Android App基座识别不到设备 mac
android·macos·智能手机
顾北川_野2 小时前
Android 手机设备的OEM-unlock解锁 和 adb push文件
android·java
hairenjing11232 小时前
在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
android·人工智能·windows·macos·智能手机
小黄人软件2 小时前
android浏览器源码 可输入地址或关键词搜索 android studio 2024 可开发可改地址
android·ide·android studio
dj15402252033 小时前
group_concat配置影响程序出bug
android·bug