如何从连续动态添加的所有EditText中获取文本

时间:2018-11-23 12:33:15

标签: android android-studio

EditText由用户动态添加。 当用户点击保存按钮,然后存储所有数据时,如何从每个动态创建的EditText中获取数据,然后存储所有这些值,由用户动态添加并另存为“共享”首选项,或者另存为数组,谢谢

public class ActivityOptions extends AppCompatActivity {
    private LinearLayout parentLinearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options);
        parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);

    }
    public void onAddField(View v) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.field, null);
        // Add the new row before the add field button.
        parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
    }

    public void onDelete(View v) {
        parentLinearLayout.removeView((View) v.getParent());
    }

activity.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="vertical" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >
        <EditText
            android:id="@+id/edit_text"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:inputType="text"/>
        <Button
            android:id="@+id/delete_button"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:background="@android:drawable/ic_delete"
            android:onClick="onDelete"/>
    </LinearLayout>
    <Button
        android:id="@+id/add_field_button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="#555"
        android:layout_gravity="center"
        android:onClick="onAddField"
        android:textColor="#FFF"
        android:text="Add Field"
        android:paddingLeft="5dp"/>
    <TextView
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:id="@+id/txt"
        android:text="HELLO"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

fiel.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="50dp"
    android:orientation="horizontal" >
    <EditText
        android:id="@+id/edit_text"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:inputType="text"/>
    <Button
        android:id="@+id/delete_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:background="@android:drawable/ic_delete"
        android:onClick="onDelete"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您可以通过循环parentView来检索值,例如

ArrayList getEditTextList()
{

    ArrayList<String> list = new ArrayList<String>();
    int size = parentLinearLayout.getChildCount()

    for (int i=0 ; i < size ; i++)
    {
      View view = parentLinearLayout.getChildAt(i);
      EditText text = view.findViewById(R.id.yourEditTextName);
      list.add(text.getText().toString());
    }
return list;
}

稍后,您可以通过将Arraylist转换为Json String或一一添加来将这些值添加到Shared首选项。