如何观察警报对话框的实时数据
How to observe live data for Alert Dialog
我从片段中打开了警报对话框,它有两个编辑文本和一个按钮,一次显示一个编辑文本。单击提交后,调用 API -> 基于响应 -> 第二个编辑文本将可见。我正在使用 ViewModel 和 LiveData 来监听响应。
问题:如何在警报对话框上下文中正确观察实时数据?
val dialogBinding: DialogTestBinding = DialogTestBinding.inflate(layoutInflater)
val dialog = AlertDialog.Builder(requireContext()).create()
dialogBinding.edtMobile.setText(viewModel.userMobile)
dialogBinding.submit.setOnClickListener {
if (dialogBinding.edtMobile.visibility == View.VISIBLE) {
//observing live data with viewLifecycleOwner - how to correctly manage for alert dialog//
viewModel.newNumber.observe(viewLifecycleOwner) {
dialogBinding.edtOtp.visibility = View.VISIBLE
dialogBinding.edtMobile.visibility = View.GONE
//viewModel.newNumber.removeObservers(viewLifecycleOwner)
}
val newNumber = dialogBinding.edtMobile.text.toString()
callAPI(newNumber, "")
} else {
val newNumber = dialogBinding.edtMobile.text.toString()
val otp = dialogBinding.edtOtp.text.toString()
callAPI(newNumber, otp)
}
}
dialog.setView(dialogBinding.root)
dialog.show()
这是有效的,但即使在关闭 AlertDialog 后它也会侦听更改。除了手动删除 Observer 之外,还有其他选择吗?
要在 Dialog
中构建复杂的逻辑,请使用 DialogFragment
。而对应的ViewModel
负责存放LiveData
.
class IpAddressEditorDialog : DialogFragment() {
private lateinit var binding: DialogIpAddressEditorBinding
private lateinit var viewModel: IpAddressEditorViewModel
private lateinit var alertDialog: AlertDialog
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
binding = DataBindingUtil.inflate(layoutInflater, R.layout.dialog_ip_address_editor, null, false)
viewModel = ViewModelProvider(this).get(IpAddressEditorViewModel::class.java)
alertDialog = getAlertDialog()
binding.viewModel = viewModel
binding.lifecycleOwner = this
return alertDialog
}
private fun getAlertDialog(): AlertDialog {
val dialogBuilder = MaterialAlertDialogBuilder(requireContext()).apply {
setView(binding.root)
setPositiveButton(R.string.confirm_label) {
//do your stuff
}
setNegativeButton(R.string.cancel_label) {
//do your stuff
}
}
return dialogBuilder.create()
}
}
我从片段中打开了警报对话框,它有两个编辑文本和一个按钮,一次显示一个编辑文本。单击提交后,调用 API -> 基于响应 -> 第二个编辑文本将可见。我正在使用 ViewModel 和 LiveData 来监听响应。
问题:如何在警报对话框上下文中正确观察实时数据?
val dialogBinding: DialogTestBinding = DialogTestBinding.inflate(layoutInflater)
val dialog = AlertDialog.Builder(requireContext()).create()
dialogBinding.edtMobile.setText(viewModel.userMobile)
dialogBinding.submit.setOnClickListener {
if (dialogBinding.edtMobile.visibility == View.VISIBLE) {
//observing live data with viewLifecycleOwner - how to correctly manage for alert dialog//
viewModel.newNumber.observe(viewLifecycleOwner) {
dialogBinding.edtOtp.visibility = View.VISIBLE
dialogBinding.edtMobile.visibility = View.GONE
//viewModel.newNumber.removeObservers(viewLifecycleOwner)
}
val newNumber = dialogBinding.edtMobile.text.toString()
callAPI(newNumber, "")
} else {
val newNumber = dialogBinding.edtMobile.text.toString()
val otp = dialogBinding.edtOtp.text.toString()
callAPI(newNumber, otp)
}
}
dialog.setView(dialogBinding.root)
dialog.show()
这是有效的,但即使在关闭 AlertDialog 后它也会侦听更改。除了手动删除 Observer 之外,还有其他选择吗?
要在 Dialog
中构建复杂的逻辑,请使用 DialogFragment
。而对应的ViewModel
负责存放LiveData
.
class IpAddressEditorDialog : DialogFragment() {
private lateinit var binding: DialogIpAddressEditorBinding
private lateinit var viewModel: IpAddressEditorViewModel
private lateinit var alertDialog: AlertDialog
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
binding = DataBindingUtil.inflate(layoutInflater, R.layout.dialog_ip_address_editor, null, false)
viewModel = ViewModelProvider(this).get(IpAddressEditorViewModel::class.java)
alertDialog = getAlertDialog()
binding.viewModel = viewModel
binding.lifecycleOwner = this
return alertDialog
}
private fun getAlertDialog(): AlertDialog {
val dialogBuilder = MaterialAlertDialogBuilder(requireContext()).apply {
setView(binding.root)
setPositiveButton(R.string.confirm_label) {
//do your stuff
}
setNegativeButton(R.string.cancel_label) {
//do your stuff
}
}
return dialogBuilder.create()
}
}