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();
    }
相关推荐
Digitally7 小时前
如何用5种实用方法将电脑上的音乐传输到安卓手机
android·智能手机·电脑
HahaGiver6668 小时前
Unity与Android原生交互开发入门篇 - 打开Unity游戏的设置
android·unity·交互
2501_915909069 小时前
WebView 调试工具全解析,解决“看不见的移动端问题”
android·ios·小程序·https·uni-app·iphone·webview
IT乐手10 小时前
android 下载管理工具类
android
2501_9151063211 小时前
App 怎么上架 iOS?从准备资料到开心上架(Appuploader)免 Mac 上传的完整实战流程指南
android·macos·ios·小程序·uni-app·iphone·webview
科技峰行者12 小时前
安卓16提前发布能否改写移动生态格局
android
蒲公英少年带我飞12 小时前
Android NDK 编译 protobuf
android
沐怡旸12 小时前
【底层机制】ART虚拟机深度解析:Android运行时的架构革命
android·面试
小禾青青13 小时前
uniapp安卓打包遇到报错:Uncaught SyntaxError: Invalid regular expression: /[\p{L}\p{N}]/
android·uni-app
studyForMokey13 小时前
【Kotlin内联函数】
android·开发语言·kotlin