Обработка большого количества данных EditText в Android - PullRequest
0 голосов
/ 25 января 2020

Я делаю что-то вроде таблицы EditText размером 5 x 9.

Поэтому я сначала дал каждому из 45 EditTexts отдельные идентификаторы, но я продолжаю думать, что с этим слишком неудобно обращаться.

Есть ли способ удобнее обрабатывать эти 45 строк ??

Ответы [ 2 ]

2 голосов
/ 25 января 2020

Вы можете просто сделать это, как показано ниже:)

Первое: объявить Id для вашего root элемента в вашем макете
(если вы работаете с фрагментами , вам не нужно объявлять идентификатор, и вы можете просто получить root Просмотреть в onViewCreated )

Второй : L oop ваш root макет и получить его дочерние тексты, если они являются EditText

Окончательный код в Деятельности:

    List<String> inputs=new ArrayList<>();
    LinearLayout rootView=findViewById(R.id.yourrootElementId);

      //if your root element type is not LineareLayout replace LinearLayout with your root 
     //element type for Example ConstraintLayout
    for (int i = 0; i < rootView.getChildCount(); i++) {
        View child = rootView.getChildAt(i);
        if (child instanceof EditText)
        {
            String text=((EditText) child).getText().toString().trim();
            inputs.add(text);
        }
    }
1 голос
/ 25 января 2020

Использовать GridLayout . Установите для columnCount значение 5, а для rowCount - 9.

<GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="5"
        android:rowCount="9"
        android:alignmentMode="alignBounds"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <EditText
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:gravity="start|top"
            android:inputType="textMultiLine"
            android:background="@drawable/box_background"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_gravity="fill"/>

        <EditText
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:gravity="start|top"
            android:inputType="textMultiLine"
            android:background="@drawable/box_background"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_gravity="fill"/>

            .......

</GridLayout>

Приведенный выше код использует весовые атрибуты для автоматического растяжения. Для этого требуется минимальный уровень API 21. Если вам не нужно автоматическое растяжение, вы все равно можете использовать GridLayout без атрибутов веса.

Если вам нужно автоматическое растяжение ниже 21, тогда вы можете использовать LinearLayout. Один вертикальный LinearLayout для всей таблицы и один горизонтальный LinearLayout для каждой строки.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/box_background"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/box_background"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/box_background"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/box_background"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/box_background"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/box_background"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/box_background"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/box_background"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/box_background"/>
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/box_background"/>
    </LinearLayout>

..........

</LinearLayout>

Результат:

enter image description here

...