前言
EditText 繼承自 TextView,擁有豐富的 XML 屬性控制輸入法行為、顯示樣式、編輯限制。本文系統整理核心屬性分類、常用組合與避坑指南,作為開發速查手冊。
核心屬性分類速查表
1. 輸入類型與輸入法行為 (inputType 為核心)
| 屬性 | 說明 | 常用值/組合 |
android:inputType | 核心屬性,決定鍵盤類型、輸入規則、旗標組合 (用 "|" 分隔) | text、textPersonName、textEmailAddress、textPassword、number、numberSigned、numberDecimal、phone、datetime、textMultiLine、textCapWords、textCapSentences、textAutoCorrect、textNoSuggestions |
android:numeric | 舊版屬性 (已棄用),功能被 inputType 取代 | integer、signed、decimal |
android:digits | 白名單字元,僅允許輸入指定字元集 | "0123456789."、"abcdefghijklmnopqrstuvwxyz" |
android:capitalize | 舊版屬性 (已棄用),自動大寫控制,改用 inputType="textCapWords|textCapSentences" | none、sentences、words、characters |
android:autoText | 舊版屬性,自動拼寫修正,改用 inputType="textAutoCorrect" | true/false |
2. 顯示與編輯行為控制
| 屬性 | 說明 | 典型值 |
android:singleLine | 強制單行 (已棄用),改用 inputType="text" + maxLines="1" | true/false |
android:maxLines / minLines | 最大/最小顯示行數,配合 inputType="textMultiLine" 實現多行輸入 | 整數 |
android:lines | 固定行數 | 整數 |
android:gravity | 文字在 View 內對齊方式 | top|start、center_vertical、bottom|end |
android:imeOptions | 輸入法編輯器動作按鈕 (右下角) | actionDone、actionNext、actionSearch、actionSend、actionGo、flagNoEnterAction |
android:editable | 是否可編輯 (仍可獲取焦點、複製),false 等同唯讀 | true/false |
android:enabled | 是否啟用 (不可編輯、不可聚焦、樣式變灰) | true/false |
android:password | 舊版屬性,密碼模式 (顯示圓點),改用 inputType="textPassword" 或 textVisiblePassword | true/false |
3. 文字外觀與樣式
| 屬性 | 說明 |
android:textColor | 文字顏色 (ColorInt/ColorRes) |
android:textSize | 文字大小 (sp) |
android:textStyle | normal、bold、italic、bold|italic |
android:typeface | normal、sans、serif、monospace (等寬) |
android:textScaleX | 字符水平縮放比例 (浮點數,1.0 為正常) |
android:letterSpacing | 字符間距 (API 21+,單位 em,如 0.1) |
android:fontFamily | 字體家族 (支援字體資源 @font/xxx、系統字體名) |
android:textColorHint | Hint 文字顏色 |
android:textColorHighlight | 選中文字背景色 |
android:textColorLink | 超連結文字顏色 |
android:shadowColor, shadowDx, shadowDy, shadowRadius | 文字陰影效果 |
4. 提示與輔助
| 屬性 | 說明 |
android:hint | 空白時顯示的提示文字 (可用 @string/...) |
android:placeholderText (API 30+) | 佔位符文字,語意上區別於 hint (不消失、無障礙語意不同) |
android:autoLink | 自動識別並超連結化:none、web、email、phone、map、all |
5. 游標、選區與選單
| 屬性 | 說明 |
android:cursorVisible | 游標是否顯示 |
android:textCursorDrawable | 自訂游標 Drawable (API 29+ 可用 @null 隱藏) |
android:textSelectHandle, textSelectHandleLeft, textSelectHandleRight | 自訂選區控制點圖示 |
android:customSelectionActionModeCallback | 自訂選區動作模式 (複製/貼上/分享等選單) |
6. 背景與邊框 (Material Design 建議使用 TextInputLayout)
| 屬性 | 說明 |
android:background | 背景 Drawable (常用 @null 移除底線、或 ShapeDrawable 圓角邊框) |
android:padding | 內邊距 |
android:drawableLeft/Start, Right/End, Top, Bottom | 複合 Drawable (圖標),配合 drawablePadding |
常見組合範例
帳號輸入框 (郵箱/手機、下一個、自動填充)
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
android:imeOptions="actionNext"
android:autofillHints="emailAddress"
android:maxLines="1"
android:textSize="16sp"
android:padding="16dp"
android:background="@drawable/bg_input_outline" />
密碼輸入框 (顯示/隱藏切換、完成動作)
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_password"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:autofillHints="password"
android:maxLines="1" />
搭配 TextInputLayout + passwordToggleEnabled="true" 可一鍵實現顯示/隱藏切換。
多行輸入框 (備註、意見回饋)
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="120dp"
android:gravity="top|start"
android:inputType="textMultiLine|textCapSentences"
android:hint="@string/hint_feedback"
android:padding="16dp"
android:background="@drawable/bg_input_outline" />
純數字輸入 (金額、驗證碼)
<!-- 金額:允許小數點 -->
<EditText
android:inputType="numberDecimal"
android:digits="0123456789."
android:maxLength="10" />
<!-- 6 位驗證碼:純數字、單行、自動聚焦下一個 (需配合自訂邏輯) -->
<EditText
android:inputType="number"
android:maxLength="6"
android:maxLines="1" />
避坑指南與最佳實踐
- 棄用屬性:
singleLine、numeric、capitalize、autoText、password 均已棄用,**統一使用 inputType 組合**。
- inputType 組合邏輯:
text 類與 number 類互斥;textMultiLine 需搭配 text 類;textPassword 隱含單行。
- IME 動作處理:在
setOnEditorActionListener 中處理 actionDone/actionNext,並手動隱藏鍵盤或切換焦點。
- 自動填充:Android 8.0+ 支援
autofillHints (如 username, password, creditCardNumber) 提升用戶體驗。
- TextInputLayout 包裹:Material Design 規範要求使用
com.google.android.material.textfield.TextInputLayout 包裹 EditText,可獲得浮動標籤、錯誤提示、字數統計、密碼切換、前後綴圖標等豐富功能。
- 軟鍵盤遮擋:Manifest Activity 設定
android:windowSoftInputMode="adjustResize|stateHidden",配合 fitsSystemWindows="true" 於根佈局。
總結
EditText 屬性體系龐大但邏輯清晰:inputType 定輸入本質、imeOptions 定動作鍵、maxLines/minLines 定行數、TextAppearance 定外觀、TextInputLayout 定容器樣式。掌握核心屬性組合與棄用替代對照,即可應對 99% 的表單輸入場景。