ViewBinding在使用自定义视图的Dialogfragment中



我有一个关于在对话框片段中使用viewBinding的问题,它使用自定义视图。有什么标准的做法吗?

我的代码使用findViewById()的对话片段,但我想使用viewbinding,因为这是整个项目的标准。

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
}
}

我试图在onCreateDialog()函数中使用binding.closebutton,但我们不工作(我假设由于片段生命周期)。

我看了这些问题:

如何正确使用Android视图绑定在对话片段?

Android DialogFragment onViewCreated not called

但仍然没有想出什么是最好的方法来实现这一点(也是我第一次使用viewbinding来自kotlin合成)。

修复。只是没有将内容视图设置为binding.root。现在可以了。https://medium.com/nerd - - tech/exploring视图绑定- -活动-片段对话框-和- recyclerview适配器- 789 f84b31a2a

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
}
}

相关内容

  • 没有找到相关文章

最新更新