Kotlin自定义Dialog

Kotlin自定义Dialog

        刚学kotlin,上班空闲之余写一个自定义dialog的例子吧。大佬们勿喷,抱拳了老铁!!

1,新建一个.kt 类继承Dialog,然后写一个布局,声明一下布局内对应的控件。

    class SafeCenterDialog : Dialog {
    var tvTitle: TextView? = null
    var ivDialogIcon: ImageView? = null
    var tvDialogContent: TextView? = null
    var btDialogConfirm: TextView? = null //确定按钮可通过外部自定义按钮内容
    var tvDialogCancel: TextView? = null //取消
    var tvphoneInput: EditText? = null//输入手机验证码
    var tvphonecode: TextView? = null//发送手机验证码
...

2,新建一个constructor构造函数,传入两个参数context,自定义的Layout布局。在这个函数里绑定控件.

 constructor(context: Context, themeStyle: Int) : super(context, themeStyle) {
        initView()
    }

    private fun initView() {
        setContentView(R.layout.dialog_safecenter)
        setCanceledOnTouchOutside(false)
        tvTitle = findViewById(R.id.tv_dialog_title)
        bindcard_isvoice = findViewById(R.id.bindcard_isvoice)
        tv_get_very_bindcard_isvoice = findViewById(R.id.tv_get_very_bindcard_isvoice)
        btDialogConfirm = findViewById(R.id.safe_sumbit)
        tvDialogCancel = findViewById(R.id.close_dialog)
        tvphoneInput = findViewById(R.id.center_input_code)
...
    }

3,新建Builder类,传入调用页面this。

 class Builder(val context: Context) {
        var confirmListener: OnConfirmListener? = null
        var cancelListener: OnCancelListener? = null
        var title: String? = null
        var icon: Int? = 0
        var content: String? = null
        var btConfirmText: String? = null
...
        fun setOnCancelListener(cancelListener: OnCancelListener): Builder {
            this.cancelListener = cancelListener
            return this
        }
        fun setTitle(title: String): Builder {
            this.title = title
            return this
        }
        fun setIcon(icon: Int): Builder {
            this.icon = icon
            return this
        }
        // 点击确定按钮的文字
        fun setConfirmText(btConfirmText: String): Builder {
            this.btConfirmText = btConfirmText
            return this
        }
        //在下面方法内做弹窗页面的操作
         fun create(): SafeCenterDialog {
            val dialog = SafeCenterDialog(context, R.style.custom_dialog2)
            dialog.window.setGravity(Gravity.BOTTOM)
            //获得window窗口的属性
            val lp = dialog.window.attributes
            //设置窗口宽度为充满全屏
            dialog.dialog_phone?.text = phonetext
            lp.width = WindowManager.LayoutParams.MATCH_PARENT
           ...
           //控件与onclick绑定
            if (cancelListener != null) {
                dialog.tvDialogCancel?.setOnClickListener { v -> cancelListener!!.onClick(dialog) }
            }
            if (confirmListener != null) {
                dialog.btDialogConfirm?.setOnClickListener { v -> confirmListener!!.onClick(dialog) }
            }

4,建立相应接口给弹窗主页面调用。

    // 点击弹窗取消按钮回调
    interface OnCancelListener {
        fun onClick(dialog: Dialog)
    }

    // 点击弹窗跳转回调
    interface OnConfirmListener {
        fun onClick(dialog: Dialog)
    }

    //发送手机验证码
    interface OnPhoneCodeListener {
        fun onClick(dialog: Dialog)
    }在这里插入代码片

5,页面使用,使用onclick方法内dialog参数对弹窗进行操作。

 SafeCenterDialog.Builder(this)
            .setGatext("0")
            .setPhonetext(PreferenceUtils.getUserName())
            .setOnPhoneCodeListener(object : SafeCenterDialog.OnPhoneCodeListener {
                override fun onClick(dialog: Dialog) {
                    verifyCode(
                        dialog.phone_code,
                        dialog.tv_get_very_bindcard_isvoice,
                        dialog.bindcard_isvoice
                    )
                    mViewModel.getVeryCode(PreferenceUtils.getMOBILE(), 0)
//                    dialog()
                }
            })
            .setType(DialogType.BIND_CARD)
            .setOnCancelListener(object : SafeCenterDialog.OnCancelListener {
                override fun onClick(dialog: Dialog) {
                    dialog.dismiss()
                }
            })
            .setOnConfirmListener(object : SafeCenterDialog.OnConfirmListener {
                override fun onClick(dialog: Dialog) {
                    mViewModel.bindWechatInfo(
                        dialog.center_mima_code.text.toString(),
                        dialog.center_input_code.text.toString(),
                        et_wechat_num.text.toString(), tempFile!!
                    )
                }

            })
            .setOnTvIsvoice(object : SafeCenterDialog.OnTvIsvoiceListener {
                override fun onClick(dialog: Dialog) {
                    dialog()
                }
            })
            .create()
            .show()

6,结束了,不知道写点儿啥,只能胡乱贴点代码。

 打扰了,告辞

你可能感兴趣的:(Kotlin自定义Dialog)