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

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

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

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

一些修改文本的活动

  1. if(intent.hasExtra("name")){
  2. university_name_campus.text=intent.getStringExtra("name")
  3. }

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

xdnvmnnf

xdnvmnnf1#

这很容易。

  1. //use this after View has been created, such as in onViewCreated
  2. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  3. super.onViewCreated(view, savedInstanceState)
  4. if(intent.hasExtra("name")){
  5. //you need to use the id of the element you want to change.
  6. // use findViewById
  7. val universityName:TextView = view.findViewById(R.id.total_univer_name)
  8. universityName.text=intent.getStringExtra("name")
  9. }
  10. }

希望这对你有所帮助。

相关问题