Android 如何在我多次插入的布局中获取对象(EditText、TextView、微调器)的值?

Android 如何在我多次插入的布局中获取对象(EditText、TextView、微调器)的值?,android,android-layout,Android,Android Layout,这是我的父布局 <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/divider1" android:orientation="vertical">

这是我的父布局

<LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/divider1"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin">

            <TextView
                android:id="@+id/principaltxt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="Principal"
                android:textColor="@color/Black" />

            <Spinner
                android:id="@+id/principal"
                android:layout_width="match_parent"
                android:layout_height="25dp"
                android:layout_marginTop="3dp"
                android:alpha="0.8"
                android:entries="@array/Principal" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight=".4"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:text="Item"
                        android:textColor="@color/Black" />

                    <Spinner
                        android:id="@+id/item"
                        android:layout_width="match_parent"
                        android:layout_height="25dp"
                        android:layout_marginTop="3dp"
                        android:alpha="0.8"
                        android:entries="@array/Product" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:text="Quantity"
                        android:textColor="@color/Black" />

                    <Spinner
                        android:id="@+id/qty"
                        android:layout_width="match_parent"
                        android:layout_height="25dp"
                        android:layout_marginTop="3dp"
                        android:alpha="0.8"
                        android:entries="@array/Quantity" />
                </LinearLayout>
            </LinearLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="Item Code"
                android:textColor="@color/Black" />

            <EditText
                android:id="@+id/itemCode"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dp"
                android:alpha="0.8"
                android:background="@drawable/edittext"
                android:textColor="@color/Black" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="Remarks per order"
                android:textColor="@color/Black" />

            <EditText
                android:id="@+id/rmrksPO"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dp"
                android:alpha="0.8"
                android:background="@drawable/edittext"
                android:textColor="@color/Black" />
        </LinearLayout>

        <View
            android:id="@+id/divider2"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/linearLayout"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="25dp"
            android:background="@android:color/darker_gray" />

    </LinearLayout>
你可以用

 ...inflatedLayout.findViewById(...)
获取特定布局的特定元素的值


因此,对于多布局,您可以使用多个视图对象,也可以在每次使用其他布局初始化视图对象之前存储元素的值

使用
列表视图
回收视图
。我将附议另一条评论-您很少希望像这样动态添加视图,因为它要求您为每个对象都有一整套视图。这在内存中是很昂贵的,尤其是当您有一个大的视图集时。有时需要动态添加视图,但通常需要使用回收器视图或列表视图,这将在对象之间回收视图并减少内存负载。它还使查找子视图变得更容易,特别是如果您遵循ViewHolder模式。这是一个前面的问题。可能重复
TextView addNew, principal;
LinearLayout linearlayout;
Spinner spnPrincipal;
View inflatedLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order_form);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    spnPrincipal = (Spinner) findViewById(R.id.principal);
    linearlayout = (LinearLayout) findViewById(R.id.linearLayout);

    principal = (TextView) findViewById(R.id.principaltxt);
    addNew = (TextView) findViewById(R.id.addNew);
    addNew.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater inflater = LayoutInflater.from(OrderForm.this);
            inflatedLayout = inflater.inflate(R.layout.add_new_item, null, false);
            linearlayout.addView(inflatedLayout);
        }
    });

}
 ...inflatedLayout.findViewById(...)