java - 在 Android 中处理大量的 EditText 数据
问题描述
我正在制作某种 5 x 9 大小的类似表格的EditText。
所以我首先给了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>
上面的代码使用权重属性进行自动拉伸。这需要最低 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>
结果:
推荐阅读
- javascript - 如何在 JavaScript 中从外部 vue 应用程序调用自定义 Vue 3 方法?
- python - 如何在 Django 中对特定月份的时间值求和?
- c - 返回指向局部变量排序合并的指针
- java - 如何配置反应式资源服务器以使用具有对称密钥的 JWT?
- javascript - 用户单击按钮时如何创建随机页面?
- javascript - 使用 Puppeteer 加载 VueJS 和 Vuetify 应用程序
- google-apps-script - Google Sheets Filtered View prevents app script to get correct cell by getRow
- python - Merge derivative terms with sympy?
- python - Python:无法安装 googletrans
- java - How to Assert same response from list in Junit using hamcrest