Как получить текст из всех EditText Динамически добавляется подряд - PullRequest
0 голосов
/ 23 ноября 2018

EditText динамически добавляется пользователем.Как получить данные из каждого динамически создаваемого EditText. Или получить значения из динамически созданных EditTexts, когда пользователь нажимает кнопку «Сохранить», затем сохраняет все из них, динамически добавленные пользователем и сохраняет как настройки общего доступа или как массив, спасибо, ребята

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 голосов
/ 23 ноября 2018

Вы можете получить значение, зацикливая 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;
}

Позже вы можете добавить значения в предпочтение Shared, либо преобразовав Arraylist в строку Json, либо добавив его по одному.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...