【发布时间】:2016-07-15 09:39:21
【问题描述】:
我想在 Android Studio 的 for 循环中更改 textViews 的文本,如下所示:
for i=0 ------------> textView0 设置文本“默认”
for i=1 ------------> textView1 设置文本“默认”
for i=2 ------------> textView2 设置文本“默认”
有可能做到吗?我不知道如何根据“i”值更改循环内的textView id,有人可以帮助我吗?
这是我的 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" />
【问题讨论】:
-
可以设置TextView对象数组
-
是的,这很有可能。您可以循环进入 TextView 父级的子视图。请显示您的 xml,以便我们看看是否是这种情况。
-
检查我的解决方案@Tymek T.
-
在我的问题中添加了 XML,@SathishKumarJ 没关系,但将来我会有更多的 textViews,所以我想自动执行此操作
-
试试Sohail Zahid的回答
标签: java android for-loop textview