【问题标题】:Adding EditText dynamically by pressing a Button通过按下按钮动态添加 EditText
【发布时间】:2019-03-22 11:50:18
【问题描述】:

我想在“数量”EditText 视图旁边添加一个“成分”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: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>

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

【问题讨论】:

  • 你想在按钮点击时只显示一个 EditText 还是想在每次按下按钮时添加一个新的?
  • 我想在每次按下按钮时添加一个新的

标签: android android-edittext android-button


【解决方案1】:
  1. “添加”按钮需要设置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);
        }
    });
}

【讨论】:

  • 谢谢。它可以工作,但是如何在我的 LinearLayout 的不同行中添加每个新的 EditText 视图?
  • @EdoardoTavilla 看看这一行:layout.addView(ingredient, 1)。第二个参数是LinearLayout 内的一个位置。更改它以将视图添加到您想要的位置。在我的示例中,我使用常量值,但您可以动态计算位置。
【解决方案2】:

第一步:将所有视图写入XML文件,并隐藏“成分”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 步:点击按钮时可见“成分”

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

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

【讨论】:

    【解决方案3】:
    EditText etIngredient = new EditText(context); // pass Context here
    etIngredient.setLayoutParams(new LayoutParams(..., ...)); // two Arguments such as LayoutParams.WRAP_CONTENT
    layout.addView(etIngredient);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多