Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 关闭带有自定义Anko布局DSL的警报对话框_Android_Kotlin_Anko - Fatal编程技术网

Android 关闭带有自定义Anko布局DSL的警报对话框

Android 关闭带有自定义Anko布局DSL的警报对话框,android,kotlin,anko,Android,Kotlin,Anko,我用一个简单的视图创建了以下警报对话框,其中包括文本视图、编辑文本和按钮: alert { customView { verticalLayout { textView { text = getString(R.string.enter_quantity) textSize = 18f

我用一个简单的视图创建了以下警报对话框,其中包括
文本视图
编辑文本
按钮

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()

每当单击按钮并且执行了
if-else
子句时,我都想取消它。我尝试使用
this@alert
但它不提供对话框方法

这是有问题的,因为您的
按钮
函数调用会在对话框还不存在时执行,该调用会注册侦听器

这里有一种方法,使用局部变量使
对话框
在侦听器中可用:

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

您还可以将生成器的结果分配给类属性等。请注意,局部变量的
lateinit
是可用的。

您只需在“是”或“否”按钮回调中执行
it.disease()
,如下所示

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

使用
DialogInterface
作为变量类型会在编辑器上显示
冲突声明
错误消息;
AlertBuilder
完整的错误消息是什么?也许您的作用域中已经有一个名为
dialog
的变量?您是对的,我有一个名为
dialog
的冲突变量。改名成功了。谢谢