如何在单击多个按钮时更改对话框内 TextView 的文本?使用科特林

How to change text of a TextView inside a dialog box on multiple button click ? using kotlin

假设我有两个按钮。 我需要打开对话框以单击按钮。 单击按钮 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()
}