在 Android 中处理大量 EditText 数据

Handling a lot of EditText data in Android

我正在制作某种类似 table 的 EditText,大小为 5 x 9。

所以我先给每个45个EditText个单独的id,但我一直觉得这样处理太不方便了。

有什么方法可以更方便的处理这45个String吗??

你可以简单地按照下面的方式做:)

首先:在布局[=24]中为您的根元素声明一个Id =] (如果你正在处理 fragments 你不需要声明 id 并且你可以简单地在 onViewCreated 中获取根视图)

第二: 循环你的根布局并获取它的子文本,如果它们是 EditText

Final code in Activity :

    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);
        }
    }

使用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。整个 table 一个垂直 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>

结果: