如何动态将“ TextInputEditText”添加到布局中?

时间:2019-08-09 06:46:44

标签: android

我正在制作一个接受产品的应用程序。而且我希望用户为每种产品输入不同的到期日期。

我已经在xml布局中创建了一张卡片,这就是它的样子。

enter image description here

我想要的是单击ADD时将显示TextInputEditText。我所做的是,我已经创建了一个布局,然后将其设置为gone,当单击按钮时,我将布局设置为visible。但是,这只会在1种布局中发生。

enter image description here

我希望用户添加直到4 TextInputEditText

如何执行此操作?请检查我的代码:

<android.support.v7.widget.CardView
                android:layout_width="650dp"
                android:layout_height="wrap_content"
                android:padding="@dimen/text_padding"
                android:layout_marginTop="@dimen/padding_top"
                android:layout_marginBottom="@dimen/padding_top"
                android:background="@color/whiteBG"
                android:layout_gravity="center">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/top"
                    android:layout_marginBottom="@dimen/top"
                    android:orientation="vertical"
                    android:paddingLeft="@dimen/padding_30"
                    android:paddingRight="@dimen/padding_30">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Expiration Date"
                        android:textColor="@color/colorPrimary"/>

                   <LinearLayout
                        android:id="@+id/exp_date_item_count_layout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:paddingTop="@dimen/padding_top"
                        android:visibility:"gone">

                        <android.support.design.widget.TextInputLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:paddingTop="@dimen/text_padding"
                            android:paddingRight="@dimen/padding_30"
                            app:errorEnabled="true">

                            <android.support.design.widget.TextInputEditText
                                android:id="@+id/textprod_exp_date"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:drawableRight="@drawable/ic_date_range"
                                android:drawablePadding="@dimen/text_padding"
                                android:drawableTint="@color/icon_light"
                                android:hint="@string/exp_date"
                                android:padding="@dimen/text_padding"
                                android:textColorHint="@color/colorPrimary"
                                android:textSize="@dimen/text_inputs" />

                        </android.support.design.widget.TextInputLayout>

                        <android.support.design.widget.TextInputLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:paddingLeft="@dimen/padding_30"
                            android:paddingTop="@dimen/text_padding"
                            app:errorEnabled="true">

                            <android.support.design.widget.TextInputEditText
                                android:id="@+id/textinput_expdatecount"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:hint="@string/item_count"
                                android:inputType="number"
                                android:padding="@dimen/text_padding"
                                android:textColorHint="@color/colorPrimary"
                                android:textSize="@dimen/text_inputs" />

                        </android.support.design.widget.TextInputLayout>

                    </LinearLayout>

                        <ImageView
                            android:id="@+id/imgview_remove"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_remove"
                            android:tint="@color/colorPrimary" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:paddingTop="@dimen/padding_top">

                        <ImageView
                            android:id="@+id/imgview_addexpdate"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_add_circle"
                            android:tint="@color/colorPrimary" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:paddingLeft="@dimen/text_padding"
                            android:text="@string/add_exp_date"
                            android:textColor="@color/dark_text" />

                    </LinearLayout>

                </LinearLayout>
            </android.support.v7.widget.CardView>

在我的onClick中,来自MainActivity

@Override
    public void onClick(View v) {
        switch (v.getId()){
          case R.id.imgview_addexpdate:
               prodlayout.setVisibility(View.VISIBLE);
          break;
        }
}

1 个答案:

答案 0 :(得分:1)

将此布局数据放入名为“ exp_date_item_count_layout”的单独文件中(或根据需要选择文件名):-

<LinearLayout
            android:id="@+id/exp_date_item_count_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="@dimen/padding_top"
            android:visibility="gone">

    <android.support.design.widget.TextInputLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingTop="@dimen/text_padding"
            android:paddingRight="@dimen/padding_30"
            app:errorEnabled="true">

        <android.support.design.widget.TextInputEditText
                android:id="@+id/textprod_exp_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableRight="@drawable/ic_date_range"
                android:drawablePadding="@dimen/text_padding"
                android:drawableTint="@color/icon_light"
                android:hint="@string/exp_date"
                android:padding="@dimen/text_padding"
                android:textColorHint="@color/colorPrimary"
                android:textSize="@dimen/text_inputs" />

    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="@dimen/padding_30"
            android:paddingTop="@dimen/text_padding"
            app:errorEnabled="true">

        <android.support.design.widget.TextInputEditText
                android:id="@+id/textinput_expdatecount"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/item_count"
                android:inputType="number"
                android:padding="@dimen/text_padding"
                android:textColorHint="@color/colorPrimary"
                android:textSize="@dimen/text_inputs" />

    </android.support.design.widget.TextInputLayout>

</LinearLayout>

之后,将您的cardview xml更改为:-

<android.support.v7.widget.CardView

    android:layout_width="650dp"
    android:layout_height="wrap_content"
    android:padding="@dimen/text_padding"
    android:layout_marginTop="@dimen/padding_top"
    android:layout_marginBottom="@dimen/padding_top"
    android:background="@color/whiteBG"
    android:layout_gravity="center"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/top"
        android:layout_marginBottom="@dimen/top"
        android:orientation="vertical"
        android:paddingLeft="@dimen/padding_30"
        android:paddingRight="@dimen/padding_30">

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Expiration Date"
            android:textColor="@color/colorPrimary"/>

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

    </LinearLayout>

<ImageView
        android:id="@+id/imgview_remove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_remove"
        android:tint="@color/colorPrimary" />

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="@dimen/padding_top">

    <ImageView
            android:id="@+id/imgview_addexpdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_add_circle"
            android:tint="@color/colorPrimary" />

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/text_padding"
            android:text="@string/add_exp_date"
            android:textColor="@color/dark_text" />

</LinearLayout>


    </android.support.v7.widget.CardView>

通知,ID为“ exp_date_item_count_layout_container”的新线性布局 现在,每当您单击添加按钮时,只需从布局exp_date_item_count_layout扩展视图,然后将其添加到exp_date_item_count_layout_container