[英]How can I return a Boolean from AlertDialog?
In the following code, it returns true immediately and ignores the result of AlertDialog...在下面的代码中,它立即返回 true 并忽略 AlertDialog 的结果...
I'm trying to make a function to produce AlertDialogs that returns a Boolean, and I want to use the function elsewhere in the program.我试图制作一个 function 来生成返回 Boolean 的 AlertDialogs,我想在程序的其他地方使用 function。
fun showTwoButtonDialog(
theContext: Context,
theTitle: String = "Yes or no?",
theMessage: String,
): Boolean {
var a = true
AlertDialog.Builder(theContext)
.setTitle(theTitle)
.setMessage(theMessage)
.setPositiveButton("Yes") { _, _ ->
a = true
}
.setNegativeButton("No") { _, _ ->
a = false
}.show()
return a
}
You can use function for that您可以为此使用 function
fun showTwoButtonDialog(
theContext: Context,
theTitle: String = "Yes or no?",
theMessage: String,
resultBoolean: (Boolean) -> Unit
) {
AlertDialog.Builder(theContext)
.setTitle(theTitle)
.setMessage(theMessage)
.setPositiveButton("Yes") { _, _ ->
resultBoolean(true)
}
.setNegativeButton("No") { _, _ ->
resultBoolean(false)
}.show()
}
function call function 拨打
showTwoButtonDialog(this,"Title","Message") { result->
if(result){
//Do whatever with result
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.