[如何在多个按钮单击的对话框内更改TextView的文本?使用kotlin

问题描述 投票:0回答:1

让我有两个按钮。我需要打开对话框以单击按钮。当单击按钮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()
    }
android kotlin android-alertdialog
1个回答
0
投票

将字符串发送到函数,然后将其放在TextView上。

btn1.setOnClickListener{ showDialog("This is Text 1") }
btn2.setOnClickListener{ showDialog("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()
}
© www.soinside.com 2019 - 2024. All rights reserved.