如何通过 setOnEditorActionListener 关闭 android 警报对话框本身?
How to dismiss android alert dialog itself via setOnEditorActionListener?
有一个包含密码 EditText
的警告对话框,我正在尝试执行与按下键盘 RETURN 按钮后按肯定按钮相同的操作。
在我的 MainActivity 中:
fun enterPwd() {
val builder = android.app.AlertDialog.Builder(this)
val password = EditText(this)
// some layout attributes about password are omitted
password.imeOptions = EditorInfo.IME_ACTION_GO
password.setOnEditorActionListener({
if(id == EditorInfo.IME_ACTION_GO) { v, id, event ->
doSomthingFunction()
}
false
})
builder.setView(password).setMessage("message")
.setPositiveButton("confirm", { doSomethingFunction() })
.setNegativeButton("cancel", { dialog, i -> }).show()
}
在setPositiveButton
后面的doSomethingFunction()中,构建器会在按下按钮后自动关闭。但是在上一个中,对话框仍然存在。我曾尝试通过 dialog = builder.show()
,然后在 setOnEditorActionListener
中的 doSomethingFunction()
之后立即被 dialog.dismiss()
解雇(如下所示),但没有效果。如何在按下 return 键后关闭此对话框?
val dialog = builder.show()
password.setOnEditorActionListener({
if(id == EditorInfo.IME_ACTION_GO) { v, id, event ->
doSomthingFunction()
dialog.dismiss()
}
false
})
您好,请在调用 doSomthingFunction() 后关闭对话框。
尝试用 setPositiveButton() 代替 doSomethingFunction()
Ram 的评论启发了我。由于我已经定义了单击肯定按钮时的操作,我只是在声明时从构建器中获取对话框,然后简单地在肯定按钮上调用 performClick()
,一切正常。
即:
val dialog = builder.setView(password).setMessage("message")
.setPositiveButton("confirm", { doSomethingFunction() })
.setNegativeButton("cancel", { dialog, i -> }).show()
password.setOnEditorActionListener({
if(id == EditorInfo.IME_ACTION_GO) { v, id, event ->
dialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick()
}
false
})
有一个包含密码 EditText
的警告对话框,我正在尝试执行与按下键盘 RETURN 按钮后按肯定按钮相同的操作。
在我的 MainActivity 中:
fun enterPwd() {
val builder = android.app.AlertDialog.Builder(this)
val password = EditText(this)
// some layout attributes about password are omitted
password.imeOptions = EditorInfo.IME_ACTION_GO
password.setOnEditorActionListener({
if(id == EditorInfo.IME_ACTION_GO) { v, id, event ->
doSomthingFunction()
}
false
})
builder.setView(password).setMessage("message")
.setPositiveButton("confirm", { doSomethingFunction() })
.setNegativeButton("cancel", { dialog, i -> }).show()
}
在setPositiveButton
后面的doSomethingFunction()中,构建器会在按下按钮后自动关闭。但是在上一个中,对话框仍然存在。我曾尝试通过 dialog = builder.show()
,然后在 setOnEditorActionListener
中的 doSomethingFunction()
之后立即被 dialog.dismiss()
解雇(如下所示),但没有效果。如何在按下 return 键后关闭此对话框?
val dialog = builder.show()
password.setOnEditorActionListener({
if(id == EditorInfo.IME_ACTION_GO) { v, id, event ->
doSomthingFunction()
dialog.dismiss()
}
false
})
您好,请在调用 doSomthingFunction() 后关闭对话框。
尝试用 setPositiveButton() 代替 doSomethingFunction()
Ram 的评论启发了我。由于我已经定义了单击肯定按钮时的操作,我只是在声明时从构建器中获取对话框,然后简单地在肯定按钮上调用 performClick()
,一切正常。
即:
val dialog = builder.setView(password).setMessage("message")
.setPositiveButton("confirm", { doSomethingFunction() })
.setNegativeButton("cancel", { dialog, i -> }).show()
password.setOnEditorActionListener({
if(id == EditorInfo.IME_ACTION_GO) { v, id, event ->
dialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick()
}
false
})