如何动态地将文本分配给特定的文本视图。 (机器人)

时间:2014-12-22 14:40:08

标签: android textview

我的布局中有很多文字视图。

当用户点击按钮时,它应该将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);

2 个答案:

答案 0 :(得分: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);
    }
}

之后,您可以通过常用迭代器在TextViews中设置文本。

答案 1 :(得分:-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>