0

如何在线性布局中获取 textViews 的文本?我基本上会将代码放在 onSavedInstance 下,在屏幕方向发生变化的情况下,我将在 linearLayout 中显示 textViews。

我修改了下面的代码

for (int i = 0; i < ll1.getChildCount(); i++) {


    }

作为

for (int i = 0; i < ll1.getChildCount(); i++) {
        TextView children = (TextView) ll1.getChildAt(i);
        String b1 = (String) children.getText().toString();
    }

我已经尝试了上面的代码,但找不到任何成功。

4

3 回答 3

1

尝试这个:

String[] bArray = new String[ll1.getChildCount()];

for (int i = 0; i < ll1.getChildCount(); i++) {
    TextView children = (TextView) ll1.getChildAt(i);
    String b = children.getText().toString();
    bArray[i] = b;
}

如果您的线性布局仅包含文本视图,这可能会起作用。否则,您将必须提供所有文本视图 id,例如 txtView1、txtView2 等,并使用 id 获取它。

编辑:

要将所有子项返回到一个字符串中,请尝试以下操作:

StringBuilder sb = new StringBuilder(ll1.getChildCount());
for (int i = 0; i < ll1.getChildCount(); i++) {
    TextView children = (TextView) ll1.getChildAt(i);
    sb.append(children.getText().toString()+" ");
}
System.out.println(sb.toString());

希望能帮助到你。

于 2013-08-29T04:24:00.850 回答
0

您可以为此使用droidQuery

$ d = $.with(lll).selectByType("android.widget.TextView");
String[] array = new String[d.size()];
int index = 0;
d.each(new Function() {
    public void invoke($ droidquery, Object... params){
        array[index] = droidquery.val().toString();
        index++;
});
//now use you array variable
于 2013-08-29T04:31:12.690 回答
0

你可以使用这样的东西。在您的布局 xml文件中,将标签设置为 txtview,如下所示

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="txtview"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="txtview"
    android:text="TextView" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="txtview"
    android:text="TextView" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="txtview"
    android:text="TextView" />

并在您的活动中使用以下代码

    LinearLayout ll1 = (LinearLayout) findViewById(your layout id);

    String[] bArray;
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0; i < ll1.getChildCount(); i++) {
        if (ll1.getChildAt(i).getTag().equals("txtview")) {
            TextView children = (TextView) ll1.getChildAt(i);
            stringBuilder.append(children.getText().toString() + "!##!");
        }
    }
    bArray = stringBuilder.toString().split("!##!");
于 2013-08-29T05:20:32.977 回答