首页 > 解决方案 > 在 Android 中处理大量的 EditText 数据

问题描述

我正在制作某种 5 x 9 大小的类似表格的EditText

所以我首先给了45 个 EditTexts中的每一个单独的 id,但我一直认为它太不方便处理了。

有什么方法可以更方便地处理这 45 个字符串吗?

标签: javaandroidandroid-edittext

解决方案


使用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>

结果:

在此处输入图像描述


推荐阅读