我有一个关于使用 viewBinding
的问题在使用自定义 View 的对话框 fragment 中。有这样做的标准方法吗?
我的代码使用 findViewById()
对于对话框 fragment ,但我想使用 View 绑定(bind),因为这是我项目其余部分的标准。
class WifiHandlerDialogFragment(private val wifiErrorType: Int): DialogFragment() {
private var _binding: DialogWifiHandlerBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
_binding = DialogWifiHandlerBinding.inflate(LayoutInflater.from(context))
val dialog = activity?.let {
Dialog(it)
}
if(dialog != null) {
dialog.window?.requestFeature(Window.FEATURE_NO_TITLE)
dialog.window?.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN)
dialog.setContentView(R.layout.dialog_wifi_handler)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
val positiveButton = dialog.findViewById<Button>(R.id.positive_button) // really want to change this to use binding
val closeButton = dialog.findViewById<Button>(R.id.close_button) // really want to change this to use binding
val dialogMessage = dialog.findViewById<TextView>(R.id.dialog_message)
positiveButton.setOnClickListener {
startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
}
closeButton.setOnClickListener {
dismiss()
}
dialogMessage.text = when (wifiErrorType) {
1 -> getString(R.string.connection_dialog_op1)
2 -> getString(R.string.connection_dialog_op2)
3 -> getString(R.string.connection_dialog_op3)
else -> getString(R.string.error)
}
}
return dialog!!
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
我试图使用 binding.closebutton
在 onCreateDialog()
功能,但我们不工作(我假设由于 fragment 生命周期)。我看过这些问题:
How to correctly use Android View Binding in DialogFragment?
Android DialogFragment onViewCreated not called
但仍然没有找到实现这一目标的最佳方法(也是我第一次使用来自 kotlin 合成的
viewbinding
)。
最佳答案
固定的。根本没有将内容 View 设置为 binding.root
.现在工作正常。 https://medium.com/nerd-for-tech/exploring-view-binding-in-activities-fragments-dialogs-and-recyclerview-adapters-789f84b31a2a
class WifiHandlerDialogFragment(private val wifiErrorType: Int): DialogFragment() {
private var _binding: DialogWifiHandlerBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
_binding = DialogWifiHandlerBinding.inflate(LayoutInflater.from(context))
val dialog = activity?.let {
Dialog(it)
}
if(dialog != null) {
dialog.window?.requestFeature(Window.FEATURE_NO_TITLE)
dialog.window?.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN)
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog.setContentView(binding.root)
binding.positiveButton.setOnClickListener {
startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
}
binding.closeButton.setOnClickListener {
dismiss()
}
binding.dialogMessage.text = when (wifiErrorType) {
1 -> getString(R.string.connection_dialog_op1)
2 -> getString(R.string.connection_dialog_op2)
3 -> getString(R.string.connection_dialog_op3)
else -> getString(R.string.error)
}
}
return dialog!!
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
关于android - 使用自定义 View 的 Dialogfragment 中的 ViewBinding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66100539/