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

标签 android kotlin android-alertdialog

让我以为我有两个按钮。
我需要打开对话框以单击按钮。
当单击按钮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/

相关文章:

android - 房间+ Dagger 2. NPE

java - 非法状态异常,应用程序找不到类中定义的方法

java - AlertDialog.Builder 显示 API 请求为 null

java - Android 中的警报框不起作用

java - 无法实现 Android 设计支持库

android - 设置按钮文本以匹配所有 Android 中的最小文本大小

java - 如何在android中不使用String Buffer的readline将其读入字符串

android - 为 Play 商店签署我的 Android 应用程序的正确方法?

android - kotlin 中缀不适用于一个参数扩展功能

java - rxjava2 - 在线程池上执行任务的简单示例,在单个线程上订阅