我的佈局中有很多textviews。如何動態地將文本分配給特定的文本視圖。 (Android)
當用戶點擊一個按鈕時,它應該將edittext中的文本設置爲textview1。如果用戶再次點擊在編輯文本的文本應該在textview2設置等textview3 textview4
.......
怎麼去旁邊的TextView當用戶點擊一個按鈕
佈局文件
...
...
...
..
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:orientation="vertical"
android:id="@+id/tvLayout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
...
...
...
..
and my activity file
初始化..
myLayout = (LinearLayout) findViewById(R.id.tvLayout);
myTextViewList = new ArrayList<TextView>();
counter =0;
for (int i = 0; i < myLayout.getChildCount(); i++)
if (myLayout.getChildAt(i) instanceof TextView)
myTextViewList.add((TextView) myLayout.getChildAt(i));
設置文本。
TextView tv = myTextViewList.get(counter);
tv.setText(ss);
請對羽絨投票留言! – abhinav
我得到的數組索引超出界限錯誤,索引爲0大小爲0 .. – Mann
您是否將所有TextView直接作爲LinearLayout的子項? – abhinav