简体   繁体   English

在for循环中TextViews文本更改

[英]TextViews text change in a for loop

I want to change textViews' texts inside for loop in Android Studio, something like that: 我想在Android Studio中的for循环中更改textViews的文本,如下所示:

for i=0 ------------> textView0 set text "Default" 对于i = 0 ------------> textView0设置文本“默认”

for i=1 ------------> textView1 set text "Default" 对于i = 1 ------------> textView1设置文本“默认”

for i=2 ------------> textView2 set text "Default" 对于i = 2 ------------> textView2设置文本“默认”

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? 我不知道如何根据“ i”值更改循环内的textView id,有人可以帮助我吗?

Here's my XML: 这是我的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" />

Performance wise good approach. 性能明智的好方法。

    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]);
        }

You could have a labels collection as well to fetch labels form. 您也可以有一个标签集合来获取标签表格。

List<String> labels = new ArrayList<>();
labels.add("Default 1");
labels.add("Default 2");
labels.add("Default 3");

int[] textViewIds = new int[3];
textViewIds[0] = R.id.text_view_a;
textViewIds[1] = R.id.text_view_b;
textViewIds[2] = R.id.text_view_c;

for (int i = 0; i < textViewIds.length; i++) {
    TextView tv = (TextView) findViewById(textViewIds[i]);
    tv.setText(labels.get(i));
}

Well, if you have all the textviews at the same hierarchy level as you currently have in your xml, you can try this solution: 好吧,如果您所有的文本视图都与xml中当前处于相同的层次结构级别,则可以尝试以下解决方案:

Wrap the whole thing up in one parent layout, say a RelativeLayout. 将整个内容包装在一个父布局中,例如RelativeLayout。 Then, loop through it: 然后,遍历它:

int numberOfTextViews = layout.getChildCount();
for(int i = 0; i < numberOfTextViews; i++) {
    ((TextView)(afterSendingMail.getChildAt(i))).setText("Default");
} 

There is another better approach to read layout, drawable and id resource ID in android 还有另一种更好的方法来读取android中的布局,drawable和id 资源ID

    String[] messages = {"one", "two", "three"};

    for (int i = 0; i < 3; i++) {
        TextView tv = (TextView) findViewById(getResources().getIdentifier("textView" + i+1, "id", getPackageName()));
        tv.setText(messages[i]);
    }

It will read id at run time. 在运行时它将读取id May be you have 100 ids then there is no need to save these id's in an array. 可能是您有100 ids则无需将这些ID保存在数组中。

Note 注意

And use getActivity() with getResources() and getPackageName() inside Fragment . 并在Fragment 中将getActivity()getResources()getPackageName() For details have a look at this and this 有关详细信息看看这个这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM