本文介紹了TextViews 文本在 for 循環中更改的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想在 Android Studio 的 for 循環中更改 textViews 的文本,如下所示:
I want to change textViews' texts inside for loop in Android Studio, something like that:
for i=0 ------------> textView0 設置文本默認"
for i=0 ------------> textView0 set text "Default"
for i=1 ------------> textView1 設置文本默認"
for i=1 ------------> textView1 set text "Default"
for 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]);
}
這篇關于TextViews 文本在 for 循環中更改的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!