gpt4 book ai didi

java - 在Android中处理大量的EditText数据

转载 作者:行者123 更新时间:2023-12-01 19:30:24 25 4
gpt4 key购买 nike

我正在制作某种类似表格的EditText,尺寸为 5 x 9。

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

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

最佳答案

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

上面的代码使用权重属性进行自动拉伸(stretch)。这需要最低 api 级别 21。如果您不需要自动拉伸(stretch),您仍然可以使用不带权重属性的 GridLayout。

如果你需要21以下的自动拉伸(stretch),那么你可以使用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

关于java - 在Android中处理大量的EditText数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59907277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com