简体   繁体   English

使用自定义 Anko 布局 DSL 关闭警告对话框

[英]Dismissing an alert dialog with custom Anko layout DSL

I have created the following alert dialog with a simple view comprising of a TextView , EditText and Button :我创建了以下带有简单视图的警告对话框,其中包含TextViewEditTextButton

alert {
            customView {
                verticalLayout {
                    textView {
                        text = getString(R.string.enter_quantity)
                        textSize = 18f
                        textColor = Color.BLACK
                    }.lparams {
                        topMargin = dip(17)
                        horizontalMargin = dip(17)
                        bottomMargin = dip(10)
                    }

                    val quantity = editText {
                        inputType = InputType.TYPE_CLASS_NUMBER
                        background = ContextCompat.getDrawable(this@ProductsList, R.drawable.textbox_bg)
                    }.lparams(width = matchParent, height = wrapContent) {
                        bottomMargin = dip(10)
                        horizontalMargin = dip(17)
                    }

                    button(getString(R.string.confirm)) {
                        background = ContextCompat.getDrawable(this@ProductsList, R.color.colorPrimary)
                        textColor = Color.WHITE
                    }.lparams(width = matchParent, height = matchParent) {
                        topMargin = dip(10)
                    }.setOnClickListener {
                        if (quantity.text.isNullOrBlank())
                            snackbar(parentLayout!!, getString(R.string.enter_valid_quantity))
                        else
                            addToCart(product, quantity.text.toString().toInt())
                    }
                }
            }
        }.show()

I want to dismiss it whenever the button is clicked and the if-else clause has been executed.每当单击按钮并执行if-else子句时,我都想关闭它。 I tried using this@alert but it doesn't provide the dialog methods.我尝试使用this@alert但它不提供对话框方法。

This is problematic because your button function call which registers the listener executes when the dialog doesn't even exist yet.这是有问题的,因为您注册侦听器的button函数调用在对话框甚至不存在时执行。

Here's one way to do it, using a local lateinit variable to make the dialog available inside the listener:这是一种方法,使用本地lateinit变量使dialog在侦听器中可用:

lateinit var dialog: DialogInterface
dialog = alert {
    customView {
        button("Click") {
            dialog.dismiss()
        }
    }
}.show()

You could also assign the result of the builder to a class property, etc. Note that lateinit for local variables is available since Kotlin 1.2 .您还可以将构建器的结果分配给类属性等。请注意,局部变量的lateinit 自 Kotlin 1.2 起可用。

You can do simply, it.dismiss() inside yes or no button callback, like this here it is DialogInterface :你可以简单地做, it.dismiss() inside yes or no button callback,像这样itDialogInterface

alert(getString(R.string.logout_message),getString(R.string.logout_title)){
        yesButton {
            // some code here
            it.dismiss()
        }
        noButton {
            it.dismiss()
        }
    }.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM