Kotlin / Anko 如何防止按钮关闭警告对话框

3
在使用 Anko 的警报构建器时,当使用 positiveButtonnegativeButton 时,似乎两者都会导致对话框关闭,即使没有调用 dismiss()。是否有任何方法可以在单击按钮后保持对话框打开状态(如果存在除 positiveButton / negativeButton 之外的类型,则也可以)?
alert {
    title = "Add Board"
    customView {
        ....
    }
    positiveButton("OK") { doSomeFunction() }
    negativeButton("Close"){}
}.show()

1
我想这是Android的AlertDialog的默认行为。 - Bob
@Bob 啊,我没意识到。我会看看是否有覆盖的方法。 - Parker
1
请查看此答案:https://dev59.com/lXE85IYBdhLWcg3wwWet#7636468 - Bob
谢谢@Bob,创建对话框后覆盖onClickListener就可以解决问题了。 - Parker
2个回答

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

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

  isCancelable = false // Disable close here
}.show()

这不会阻止使用按钮关闭警报。 - Jose Luis Berrocal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接