繁体   English   中英

使用android中的添加按钮将editTexts动态添加到布局

[英]Add editTexts to a layout dynamically using an add button in android

我有一个带有单个editText的布局,在editText下方添加了添加按钮。 现在我的问题是,当我单击添加按钮时,我需要在editText下面获得另一个editText,并且必须重复相同的操作。请任何人帮助,谢谢。

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/ll_edit_texts_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/et1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/buttonAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD" />
</LinearLayout>

并在您的Activity以下代码

final LinearLayout llEditTextsContainer = (LinearLayout) view.findViewById(R.id.ll_edit_texts_container);
        Button buttonAdd = (Button) view.findViewById(R.id.buttonAdd);
        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText = new EditText(getActivity());
                //editText.setId(); you should set id smartly if you wanted to use data from this edittext
                llEditTextsContainer.addView(editText);
            }
        });

首先,在您的主布局中创建一个容器视图,该视图将容纳新的Edittexts。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.vuclip.dynamictextedit.MainActivity">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/addTextView"
    android:text="Add EditText"
    android:onClick="onClick"
    />

<TableLayout
    android:id="@+id/containerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</TableLayout>

在这里,按下按钮,将创建新的Edittexts并将其添加到TableLayout中。

现在创建一个新的Layout,它将保存您的edittext(我将此文件称为new_layout.xml)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
    android:id="@+id/newEditText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

现在,将此布局添加到您的主要活动中。

公共类MainActivity扩展了AppCompatActivity {

TableLayout container;
static int rowIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    container = (TableLayout) findViewById(R.id.containerLayout);
}

public void onClick(View view) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View newRowView = inflater.inflate(R.layout.new_layout,null);
    container.addView(newRowView,rowIndex);
    rowIndex++;
}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM