本文介绍了在for循环中TextViews文本更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android Studio中为循环更改textViews的文本,诸如此类:

I want to change textViews' texts inside for loop in Android Studio, something like that:

i = 0 = ------------> textView0设置文本默认"

for i=0 ------------> textView0 set text "Default"

对于i = 1 ------------> textView1设置文本默认"

for i=1 ------------> textView1 set text "Default"

对于i = 2 ------------> textView2将文本设置为默认"

for i=2 ------------> textView2 set text "Default"

有可能这样做吗?我不知道如何根据"i"值更改循环内的textView id,有人可以帮我吗?

Is it possible to do that? I don't know how to change the textView id inside the loop according to "i" value, can someone help me?

这是我的XML:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="--------------"
    android:id="@+id/textView0"
    android:layout_marginLeft="16dp"
    android:layout_gravity="center_horizontal"
    android:textSize="13dp" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="--------------"
    android:id="@+id/textView1"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="16dp"
    android:textSize="13dp" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="--------------"
    android:id="@+id/textView2"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="16dp"
    android:textSize="13dp" />

推荐答案

性能明智的好方法.

    int[] textViews = {R.id.textView1, R.id.textView2, R.id.textView3};
    String[] messages = {"one", "two", "three"};

     for (int i = 0; i < 3; i++) {
            TextView tv = (TextView) findViewById(textViews[i]);
            tv.setText(messages[i]);
        }

这篇关于在for循环中TextViews文本更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 00:31
查看更多