0
這是我的父母佈局。如何在插入多次的佈局中獲取對象(EditText,TextView,Spinner)的值?
<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" />
的事情是,我不知道我怎樣才能在我插入子佈局的任意值。請幫幫我。謝謝。請注意,我將多次插入此佈局。謝謝。
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);
}
});
}
使用'ListView'或'RecyclerView'。 –
我打算再次發表其他評論 - 您很少希望像這樣動態添加視圖,因爲它需要您爲每個對象都提供一組完整的視圖。這在內存中很昂貴,特別是如果你有大量的視圖。這是您需要動態添加視圖的時間,但通常您希望使用回收視圖或列表視圖,這將回收對象之間的視圖並減少內存負載。它還使查找子視圖更容易,尤其是如果您遵循ViewHolder模式。 –