【发布时间】:2023-03-31 08:42:01
【问题描述】:
我知道我可以从使用对话框中用户输入的 MaterialAlertDialogBuilder 内部调用函数,但我想知道是否有一种方法(最好是一种好的做法)来等待用户输入值和应用程序执行后续代码。我尝试使用 async .await() 和 kotlin-coroutines 但没有弄明白。
这是 MaterialAlertDialogBuilder 实现的示例。
val nameInputDialog = MaterialAlertDialogBuilder(requireContext())
val customAlertDialogView = LayoutInflater.from(requireContext())
.inflate(R.layout.dialod_input_et, null, false)
val inputNameDialog: EditText = customAlertDialogView.findViewById(R.id.input_dialog_et)
nameInputDialog.setView(customAlertDialogView)
.setTitle("Type the name")
.setPositiveButton("Accept") { dialog, _ ->
val nameToInput = inputNameDialog.text.toString()
if (nameToInput == "") {
Toast.makeText(requireContext(), "Type a name", Toast.LENGTH_SHORT)
.show()
makeDialogBoxAndSetGroupID()
} else if (nameToInput != "") {
GlobalScope.launch() {
nameToDisplay = async {
nameToInput
}
}
dialog.dismiss()
}
}
.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
}.show()
【问题讨论】:
-
信息不足。请通过代码示例说明您要暂停的内容。
-
@rupinderjeet 这是我的对话框实现的更新问题
-
您究竟需要等待什么?
标签: android kotlin dialog kotlin-coroutines