本文介紹了TextViews 文本在 for 循環(huán)中更改的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我想在 Android Studio 的 for 循環(huán)中更改 textViews 的文本,如下所示:
I want to change textViews' texts inside for loop in Android Studio, something like that:
for i=0 ------------> textView0 設(shè)置文本默認(rèn)"
for i=0 ------------> textView0 set text "Default"
for i=1 ------------> textView1 設(shè)置文本默認(rèn)"
for i=1 ------------> textView1 set text "Default"
for i=2 ------------> textView2 設(shè)置文本默認(rèn)"
for i=2 ------------> textView2 set text "Default"
有可能做到嗎?我不知道如何根據(jù)i"值更改循環(huán)內(nè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]);
}
這篇關(guān)于TextViews 文本在 for 循環(huán)中更改的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!