【问题标题】:How change the message on TextView of a custom Dialog Box?如何更改自定义对话框的 TextView 上的消息?
【发布时间】:2021-09-23 16:15:45
【问题描述】:

我想创建一个自定义对话框。由于我是初学者,所以参考了这个链接:https://www.tutorialspoint.com/how-to-make-custom-dialog-with-rounded-corners-in-android

但是,我无法以编程方式更改 TextView 中的消息,因为它取决于用户选择的内容。我该怎么做? 我尝试了 body_message.text = message (如下面的代码所述),但它不起作用。提前谢谢!

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp"
        android:background="@drawable/background_dialog"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Success"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline" />
        <TextView
            android:id="@+id/body_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eu erat tincidunt lacus fermentum rutrum."
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
        <Button
            android:id="@+id/buttonOk"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="15dp"
            android:backgroundTint="@color/teal_700"
            android:text="Fechar"
            android:textColor="@color/white" />
    </LinearLayout>
</LinearLayout>


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val message = "testing message"

        findViewById<View>(R.id.customDialog).setOnClickListener { v ->
            val builder: AlertDialog.Builder = AlertDialog.Builder(this@MainActivity, R.style.CustomAlertDialog)
            val viewGroup = findViewById<ViewGroup>(android.R.id.content)
            val dialogView: View = LayoutInflater.from(v.context).inflate(R.layout.custom_dialog, viewGroup, false)
            val buttonOk = dialogView.findViewById<Button>(R.id.buttonOk)
            val body_message = findViewById<TextView>(R.id.body_message)

            body_message.text = message

            builder.setView(dialogView)
            val alertDialog: AlertDialog = builder.create()
            buttonOk.setOnClickListener { alertDialog.dismiss() }
            alertDialog.show()
        }
    }
}

【问题讨论】:

    标签: android kotlin dialog


    【解决方案1】:

    我想我在多次尝试后发现了自己的错误(我还是个初学者):

    我用过 val body_message = dialogView.findViewById(R.id.body_message) 其中 body_message 是我要更改消息的 TextView,而 dialogView 是对话框。唯一的区别是我添加了“dialogView”。如果我理解正确,它会使代码正确地在对话框中查找 TextView(名为 body_message)。 它正在工作,我可以更改以编程方式显示的消息。

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 我在答案中添加了更多细节以使其更清晰。仍在学习如何完全自定义对话框,但逐步学习。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多