让我以为我有两个按钮。
我需要打开对话框以单击按钮。
当单击按钮1时,将出现一些文本,而当单击按钮2时,将出现其他文本作为对话框。
我的密码
btn1.setOnClickListener{ showCustomDialog() }
btn2.setOnClickListener{ showCustomDialog() }
private lateinit var alertDialog: AlertDialog
fun showCustomDialog() {
val inflater: LayoutInflater = this.getLayoutInflater()
val dialogView: View = inflater.inflate(R.layout.dialog_custom_view, null)
val headerbtn = dialogView.findViewById<TextView>(R.id.header)
headerbtn.text = "Header Message"
val dialogBuilder: AlertDialog.Builder = AlertDialog.Builder(context!!)
dialogBuilder.setOnDismissListener(object : DialogInterface.OnDismissListener {
override fun onDismiss(arg0: DialogInterface) {
}
})
dialogBuilder.setView(dialogView)
alertDialog = dialogBuilder.create();
alertDialog.window!!.getAttributes().windowAnimations = R.style.PauseDialogAnimation
alertDialog.show()
}
最佳答案
将字符串发送到函数,然后将其放在TextView上。
btn1.setOnClickListener{ showCustomDialog("This is Text 1") }
btn2.setOnClickListener{ showCustomDialog("This is Text 2") }
功能说明
fun showCustomDialog(data: String) {
val inflater: LayoutInflater = this.getLayoutInflater()
val dialogView: View = inflater.inflate(R.layout.dialog_custom_view, null)
val headerbtn = dialogView.findViewById<TextView>(R.id.header)
headerbtn.text = data
val dialogBuilder: AlertDialog.Builder = AlertDialog.Builder(context!!)
dialogBuilder.setOnDismissListener(object : DialogInterface.OnDismissListener {
override fun onDismiss(arg0: DialogInterface) {
}
})
dialogBuilder.setView(dialogView)
alertDialog = dialogBuilder.create();
alertDialog.window!!.getAttributes().windowAnimations = R.style.PauseDialogAnimation
alertDialog.show()
}
关于android - 如何在多个按钮单击的对话框中更改TextView的文本?使用kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59589316/