Android 使用customview在Dialogfragment中进行ViewBinding

Android 使用customview在Dialogfragment中进行ViewBinding,android,android-dialogfragment,android-dialog,android-viewbinding,Android,Android Dialogfragment,Android Dialog,Android Viewbinding,我有一个关于在使用自定义视图的对话框片段中使用viewBinding的问题。有没有一个标准的方法可以做到这一点 我的代码使用findViewById()作为对话框片段,但我希望使用viewbinding,因为这是我项目其余部分的标准 class WifiHandlerDialogFragment(private val wifiErrorType: Int): DialogFragment() { private var _binding: DialogWifiHandlerBinding?

我有一个关于在使用自定义视图的对话框片段中使用
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
}
}
类WifiHandlerDialogFragment(private val wifiErrorType:Int):DialogFragment(){
私有变量绑定:DialogWifiHandlerBinding?=null
//此属性仅在onCreateView和之间有效
//onDestroyView。
private val binding get()=\u binding!!
重写FunonCreateDialog(savedInstanceState:Bundle?:对话框){
_binding=对话框wifihandlerbinding.flate(LayoutInflater.from(上下文))
val对话框=活动?让我们{
对话(it)
}
如果(对话框!=null){
dialog.window?.requestFeature(window.FEATURE\u NO\u TITLE)
dialog.window?.setFlags(WindowManager.LayoutParams.FLAG_全屏,WindowManager.LayoutParams.FLAG_布局_屏幕)
dialog.setContentView(R.layout.dialog\u wifi\u处理程序)
dialog.window?.setBackgroundDrawable(可彩色绘制(彩色.透明))
val positiveButton=dialog.findViewById(R.id.positive_按钮)//是否确实要将其更改为使用绑定
val closeButton=dialog.findviewbyd(R.id.close_button)//是否确实要将其更改为使用绑定
val dialogMessage=dialog.findviewbyd(R.id.dialog\u消息)
positiveButton.setOnClickListener{
startActivity(意图(设置、动作\u WIFI\u设置))
}
closeButton.setOnClickListener{
解雇
}
dialogMessage.text=何时(wifiErrorType){
1->getString(R.string.connection\u对话框\u op1)
2->getString(R.string.connection\u对话框\u op2)
3->getString(R.string.connection\u对话框\u op3)
else->getString(R.string.error)
}
}
返回对话框!!
}
重写onDestroyView(){
super.onDestroyView()
_binding=null
}
}
我试图在
onCreateDialog()
函数中使用
binding.closebutton
,但我们没有工作(我假设是由于片段生命周期)

我已研究过这些问题:


但仍然没有找到实现这一点的最佳方法(这也是我第一次使用kotlin synthetics的
viewbinding

修复。只是没有将内容视图设置为
binding.root
。现在一切正常

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