动态添加EditText并获取数据存入ArrayList的实现需求
Solution: Dynamic EditText Creation & Data Collection to ArrayList
Hey there! Let's walk through how to implement this feature where tapping an "Add" button creates as many EditTexts as you need, then collect all their input values into an ArrayList.
Step 1: Set Up Your Layout
First, create a layout that includes a button and a container (like a vertical LinearLayout) to hold the dynamically added EditTexts. Here's a sample 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" android:padding="16dp"> <!-- Button to add new EditTexts --> <Button android:id="@+id/add_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add EditText"/> <!-- Container for dynamic EditTexts --> <LinearLayout android:id="@+id/edit_text_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="16dp"/> </LinearLayout>
Step 2: Implement Dynamic EditText Creation (Java)
In your Activity/Fragment, set up the button click listener to create new EditText instances, configure their properties, and add them to the container. We'll also add a method to collect all input values later:
import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private LinearLayout editTextContainer; private ArrayList<String> inputValues = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextContainer = findViewById(R.id.edit_text_container); Button addBtn = findViewById(R.id.add_btn); addBtn.setOnClickListener(v -> { // Create new EditText with layout configuration EditText newEditText = new EditText(this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ); params.setMargins(0, 8, 0, 8); // Add vertical spacing between EditTexts newEditText.setLayoutParams(params); newEditText.setHint("Enter text here"); // Add the new EditText to the container editTextContainer.addView(newEditText); }); } // Call this method when you need to collect values (e.g., on a "Save" button click) private void collectInputValues() { inputValues.clear(); // Clear old entries first for (int i = 0; i < editTextContainer.getChildCount(); i++) { View child = editTextContainer.getChildAt(i); if (child instanceof EditText) { String text = ((EditText) child).getText().toString().trim(); if (!text.isEmpty()) { inputValues.add(text); } } } // Now inputValues holds all non-empty text from dynamic EditTexts } }
Step 2 Alternative: Kotlin Implementation
If you're using Kotlin, here's the cleaner equivalent code:
import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.LinearLayout import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private lateinit var editTextContainer: LinearLayout private val inputValues = arrayListOf<String>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) editTextContainer = findViewById(R.id.edit_text_container) val addBtn = findViewById<Button>(R.id.add_btn) addBtn.setOnClickListener { val newEditText = EditText(this).apply { layoutParams = LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ).apply { setMargins(0, 8, 0, 8) } hint = "Enter text here" } editTextContainer.addView(newEditText) } } private fun collectInputValues() { inputValues.clear() for (i in 0 until editTextContainer.childCount) { val child = editTextContainer.getChildAt(i) if (child is EditText) { val text = child.text.toString().trim() if (text.isNotEmpty()) { inputValues.add(text) } } } // Use inputValues for storage or processing } }
Visual Behavior
- Before clicking "Add": The container below the button is completely empty, with no EditTexts displayed.
- After clicking "Add": Each tap adds a new, properly spaced EditText to the container, ready for user input.
Quick Tips
- You can use
ConstraintLayoutinstead ofLinearLayoutas the container—just adjust the layout parameters to fit your design. - If you want to allow removing individual EditTexts, you can add a delete button next to each dynamic EditText and handle its click to remove the view from the container.
- The whitespace trim and empty value check in
collectInputValues()keeps your ArrayList clean—remove those lines if you need to include empty entries.
内容的提问来源于stack exchange,提问作者Rashiq
相关产品推荐
相关产品推荐

