如何在活动中更改资源中的文本?

np8igboo  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(218)

我想将资源中的文本更改为活动中更改的文本。
我在活动中尝试访问的内容。university\u name\u campus.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView   xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="abc"
    android:textSize="15dp"
    android:id="@+id/total_univer_name"
    >
</TextView>

一些修改文本的活动

if(intent.hasExtra("name")){
            university_name_campus.text=intent.getStringExtra("name")

        }

最后,我想把大学校园的“”改为“活动”。

xdnvmnnf

xdnvmnnf1#

这很容易。

//use this after View has been created, such as in onViewCreated

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if(intent.hasExtra("name")){
            //you need to use the id of the element you want to change.
            // use findViewById

            val universityName:TextView = view.findViewById(R.id.total_univer_name)
            universityName.text=intent.getStringExtra("name")

        }
}

希望这对你有所帮助。

相关问题