如何在自定义对话框布局中使用视图绑定?
我在自定义对话框布局中实现视图绑定时遇到问题。是否可以?
private fun showCustomDialog(title: String) {
val dialog = Dialog(activity)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(R.layout.custom_layout)
val body = dialog.findViewById(R.id.body) as TextView
body.text = title
val noBtn = dialog.findViewById(R.id.noBtn) as TextView
yesBtn.setOnClickListener {
dialog.dismiss()
}
val yesBtn = dialog.findViewById(R.id.yesBtn) as Button
noBtn.setOnClickListener { dialog.dismiss() }
dialog.show()
}
回答
有可能的。
CustomDialogBinding binding = CustomDialogBinding
.inflate(LayoutInflater.from(getContext()));
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setView(binding.getRoot());
其中 CustomDialogBinding 是自定义布局的视图绑定文件的名称
科特林
val bind :CustomDialogBinding = CustomDialogBinding .inflate(inflater)
dialog.setContentView(bind.root)