【问题标题】:Kotlin / Anko prevent button from closing Alert DialogKotlin / Anko 防止按钮关闭警报对话框
【发布时间】:2017-08-15 15:25:20
【问题描述】:

在 Anko 的警报生成器中使用 positiveButtonnegativeButton 时,即使未调用 dismiss(),它们似乎都会导致对话框关闭。有没有什么办法让点击按钮后对话框保持打开状态(如果有positiveButton/negativeButton以外的类型也可以)?

alert {
    title = "Add Board"
    customView {
        ....
    }
    positiveButton("OK") { doSomeFunction() }
    negativeButton("Close"){}
}.show()

【问题讨论】:

  • 我想这是 Android 的 AlertDialog 的默认行为。
  • @Bob 啊,没有意识到这一点。我去看看有没有办法覆盖
  • 感谢@Bob,在创建对话框后覆盖onClickListener 就可以了

标签: android kotlin android-alertdialog anko


【解决方案1】:

对于将来可能遇到此问题的任何人,这是您可以在 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()
    }

【讨论】:

    【解决方案2】:
    alert {
      title = "Add Board"
      customView {
        ....
      }
      positiveButton("OK") { /*Keep blank, we'll override it later*/}
      negativeButton("Close"){}
    
      isCancelable = false // Disable close here
    }.show()
    

    【讨论】:

    • 这不会阻止警报被按钮关闭
    猜你喜欢
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多