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

How can you change the text in a resource in an activity?

我想将资源中的文本更改为 activity 中更改的文本。

我在 activity 中尝试访问的内容。 university_name_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")
            
        }

最终,我想将 university_name_campus 的“abc”从 activity 更改为。

很简单。

//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")
           
        }
}

希望这对您有所帮助。