Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 TableLayout 打造 Android 註冊頁面:inputType 一次說清

編輯:關於 Android 開發

註冊頁幾乎是每個 App 的標配。這裡用 TableLayout 配合 TableRow,把「標籤 + 輸入框」按行對齊排列,既能保證整齊,又能自動適配不同屏幕寬度。

頁面效果如下:

布局代碼

關鍵在於根節點的 android:stretchColumns="1",它讓第二列(輸入框)自動拉伸填滿剩餘寬度,這樣不管文字長短,右邊界都能對齊。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1"
    android:background="@drawable/bluesky">

    <!-- 賬號 -->
    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="賬號:"
            android:textSize="16sp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="手機號"
            android:selectAllOnFocus="true" />
    </TableRow>

    <!-- 密碼 -->
    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="密碼:"
            android:textSize="16sp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberPassword" />
    </TableRow>

    <!-- 生日 -->
    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="生日:"
            android:textSize="16sp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="date" />
    </TableRow>

    <!-- 住址 -->
    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="住址:"
            android:textSize="16sp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPostalAddress" />
    </TableRow>

    <!-- 電子郵箱 -->
    <TableRow>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="電子郵箱:"
            android:textSize="16sp" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress" />
    </TableRow>

    <!-- 註冊 -->
    <TableRow>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="註冊" />
    </TableRow>

</TableLayout>

inputType 的價值

這個例子最值得借鑑的地方,是用 android:inputType 為不同字段匹配合適的鍵盤與輸入約束,這一步做對了,用戶的輸入體驗會好很多:

  • numberPassword:只彈出數字鍵盤,同時以密文顯示,適合純數字密碼;
  • date:提示系統這是一個日期字段,部分鍵盤會提供日期相關的輸入輔助;
  • textPostalAddress:地址輸入專用,鍵盤行為更貼合填寫地址的場景;
  • textEmailAddress:鍵盤會自帶 @ 和 . 等符號,輸入郵箱更順手。

其他常用的還有 phone(電話號碼)、number(純數字)、textPassword(通用密文)、textMultiLine(多行輸入)等。

兩個實戰補充

密碼字段建議再補一個 android:maxLength,從源頭限制超長輸入,避免後端截取帶來的不一致。

如果這個表單要長期維護,更推薦改用 LinearLayout 或 ConstraintLayout。TableLayout 在列數固定、需要嚴格對齊的場景下很合適,但當字段較多、還需要錯誤提示文案和異步校驗狀態時,其他布局配合 TextInputLayout 會更好維護。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved