TextViews text change in a for loop

2019-02-19 18:03发布

问题:

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

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

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

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

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?

Here's my 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" />

回答1:

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


回答2:

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


回答3:

Well, if you have all the textviews at the same hierarchy level as you currently have in your xml, you can try this solution:

Wrap the whole thing up in one parent layout, say a RelativeLayout. Then, loop through it:

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


回答4:

There is another better approach to read layout, drawable and id resource ID in android

    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. May be you have 100 ids then there is no need to save these id's in an array.

Note

And use getActivity() with getResources() and getPackageName() inside Fragment. For details have a look at this and this