通过按下按钮动态添加 EditText

Adding EditText dynamically by pressing a Button

我想在 "quantity" EditText 视图旁边添加一个 "ingredient" EditText 视图,按下 "add" 按钮。这是起始布局代码:

<LinearLayout android:id="@+id/ingredients_line_layout"
    android:layout_below="@+id/title_line_layout"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/layout_margin"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <EditText
        android:id="@+id/ingredientsField"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/boxes_margin"
        android:hint="@string/ingredients"
        android:inputType="text" />

    <EditText
        android:hint="@string/quantity"
        android:id="@+id/quantityField"
        android:inputType="number"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/boxes_margin"
        android:layout_width="wrap_content" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/add_ingredient_btn"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/boxes_margin"
        android:layout_width="wrap_content"
        android:text="@string/add"
        app:icon="@drawable/ic_add_ingr_btn" />

</LinearLayout>

如何在相应的 activity 中实现按钮的 onClick 方法?

第一步:在XML文件中写入所有视图,并隐藏"ingredient"EditText

<LinearLayout android:id="@+id/ingredients_line_layout"
    android:layout_below="@+id/title_line_layout"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/layout_margin"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <EditText
        android:hint="@string/quantity"
        android:id="@+id/quantityField"
        android:inputType="number"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/boxes_margin"
        android:layout_width="wrap_content" />

    <EditText
        android:id="@+id/ingredientsField"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/boxes_margin"
        android:hint="@string/ingredients"
        android:visibility="gone"
        android:inputType="text" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/add_ingredient_btn"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/boxes_margin"
        android:layout_width="wrap_content"
        android:text="@string/add"
        app:icon="@drawable/ic_add_ingr_btn" />

</LinearLayout>

步骤 2: 在添加按钮上注册 onClick 侦听器。

addIngredientBtn.setOnClickListener(this);

第 3 步:点击按钮时可见"ingredient"

@Override
public void onClick(View v) {
    ingredientsField.setVisibility(View.VISIBLE);
}

通过这种方式,您可以动态添加EditText。

EditText etIngredient = new EditText(context); // pass Context here
etIngredient.setLayoutParams(new LayoutParams(..., ...)); // two Arguments such as LayoutParams.WRAP_CONTENT
layout.addView(etIngredient);

如果有两个编辑文本并排放置,您可能想相应地为包含 editText 的布局添加权重。

  1. 您需要为 "add" 按钮设置 OnClickListener
  2. 创建要添加的视图;
  3. 找到 ingredients_line_layout 布局并将视图添加到其中。

查看下面的代码:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    View button = findViewById(R.id.add_ingredient_btn);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ViewGroup layout = (ViewGroup) findViewById(R.id.ingredients_line_layout);
            EditText ingredient = new EditText(YourActivity.this);
            ingredient.setHint(R.string.ingredients);
            layout.addView(ingredient, 1);
        }
    });
}