这是我的父布局。
<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>
当我点击我的按钮时,我在父布局中插入了一个布局。这是我单独创建的子布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
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: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>
问题是我不知道如何在我插入的子布局中获得任何值。请注意,我将多次插入此布局。
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);
}
});
}
答案 0 :(得分:1)
您可以使用
...inflatedLayout.findViewById(...)
获取特定布局的特定元素的值。
因此,对于多个布局,您可以使用多个查看对象,或者每次使用其他布局初始化视图对象之前,您都可以存储元素的值。