2014-12-22 56 views
0

我的佈局中有很多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); 

回答

-1

試試這個:

private ArrayList<TextView> getChildTextViews(LinearLayout myLayout) 
{ 
    for(int i = 0; i < myLayout.getChildCount(); i++) 
    { 
    View child = myLayout.getChildAt(i); 
    if(child instanceof LinearLayout) 
     getChildTextViews((LinearLayout) child); 
    else if(child instanceof TextView) 
     myTextViewList.add((TextView)child); 
    } 
} 



LinearLayout myLayout = (LinearLayout) findViewById(R.id.lineraLayout); 
ArrayList<TextView> myTextViewList = getChildTextViews(myLayout); 
int counter = 0; 
Button button= (Button) findViewById(R.id.buttonId); 
button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    EditText et=(EditText)findViewById(R.id.editText); 
    String txt = et.getText().toString(); 
    TextView tv = myTextViewList.get(counter); 
    tv.setText(txt); 
    counter++; 
    } 
}); 

這是你得到你的佈局ID:

<LinearLayout android:id="@+id/linearLayout" > 
    //Here is where your TextViews would be declared 
</LinearLayout> 
+0

請對羽絨投票留言! – abhinav

+0

我得到的數組索引超出界限錯誤,索引爲0大小爲0 .. – Mann

+0

您是否將所有TextView直接作爲LinearLayout的子項? – abhinav

0

爲您的佈局運行此ViewGroup和所有TextViews將在mTextViews中。

List<TextView> mTextViews = new ArrayList(); 

void getAllTextViews(ViewGroup v) { 
    for (int i = 0; i < v.getChildCount(); i++) { 
    View child = v.getChildAt(i); 
    if(child instanceof ViewGroup) 
     getAllTextViews((ViewGroup)child); 
    else if(child instanceof TextView) 
     mTextViews.add((TextView)child); 
    } 
} 

之後,您可以通過普通的迭代器在TextView中設置文本。

+0

我只有一個按鈕。用戶在編輯文本中輸入內容,然後驗證它是否正確,它將設置爲textview1,用戶在同一個edittext中輸入內容並點擊相同的按鈕,我相應地驗證並設置文本。 – Mann

+0

這是我的複製錯誤,很抱歉。見編輯的答案。 – grig

相關問題