Kotlin/Anko阻止按钮关闭警报对话框

Par*_*ker 3 android android-alertdialog kotlin anko

在Anko的警报构建器中使用positiveButtonnegativeButton时,即使dismiss()未调用,它们似乎也会导致关闭对话框.有没有办法在单击按钮后保持对话框打开(如果有除positiveButton/ 之外的类型negativeButton,那也没关系)?

alert {
    title = "Add Board"
    customView {
        ....
    }
    positiveButton("OK") { doSomeFunction() }
    negativeButton("Close"){}
}.show()
Run Code Online (Sandbox Code Playgroud)

Par*_*ker 7

对于任何可能在将来遇到此问题的人来说,这就是您在Kotlin中实现这一目标的方法

val myAlert = alert {
    title = "Add Board"
    customView {
        ....
    }
    positiveButton("OK") { /*Keep blank, we'll override it later*/}
    negativeButton("Close"){}
    }.show()

//You can use BUTTON_NEGATIVE and BUTTON_NEUTRAL for other buttons
(myAlert as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE)
    .setOnclickListener{
        doSomeFunction()
    }
Run Code Online (Sandbox Code Playgroud)