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

EditText 的 inputType 屬性總整理

編輯:關於 Android 程式設計

android:inputType 決定 EditText 會彈出哪一種鍵盤、以及輸入內容會被如何處理。設定正確能大幅改善使用者體驗——輸入 Email 時直接看到 @ 按鍵、輸入網址時看到 / 和 .com、輸入密碼時自動隱藏字元。

一、none

android:inputType="none"

不允許輸入任何內容,同時也不會彈出軟鍵盤。通常用於純展示用途、或需要完全自訂輸入行為的場景。

二、文字類型

android:inputType="text"                  // 純文字,包含大小寫、數字與符號
android:inputType="textCapCharacters"     // 所有字母大寫
android:inputType="textCapWords"          // 每個單字首字母大寫
android:inputType="textCapSentences"      // 僅第一句的第一個字母大寫
android:inputType="textAutoCorrect"       // 自動更正拼字
android:inputType="textAutoComplete"      // 自動完成
android:inputType="textMultiLine"         // 多行輸入
android:inputType="textImeMultiLine"      // 若輸入法支援,讓 IME 也能多行
android:inputType="textNoSuggestions"     // 不顯示拼字建議
android:inputType="textUri"               // 網址
android:inputType="textEmailAddress"      // 電子郵件地址
android:inputType="textEmailSubject"      // 郵件主題
android:inputType="textShortMessage"      // 短訊
android:inputType="textLongMessage"       // 長訊息
android:inputType="textPersonName"        // 人名
android:inputType="textPostalAddress"     // 地址
android:inputType="textPassword"          // 密碼
android:inputType="textVisiblePassword"   // 可見密碼
android:inputType="textWebEditText"       // 作為網頁表單的文字
android:inputType="textFilter"            // 文字篩選過濾
android:inputType="textPhonetic"          // 拼音輸入

三、數值類型

android:inputType="number"           // 純數字
android:inputType="numberSigned"     // 帶正負號的數字
android:inputType="numberDecimal"    // 帶小數點的浮點格式
android:inputType="phone"            // 撥號鍵盤
android:inputType="datetime"         // 日期與時間
android:inputType="date"             // 日期鍵盤
android:inputType="time"             // 時間鍵盤

四、可以組合使用

inputType 支援用「|」串接多個值,這點很多人不知道:

android:inputType="textPassword|textMultiLine"     // 多行密碼輸入
android:inputType="textCapWords|textAutoComplete" // 人名自動完成且首字母大寫
android:inputType="numberDecimal|numberSigned"    // 可為負數的小數

五、實作 EditText 的下拉建議列表

想在 EditText 下方做一個建議清單,最簡單的做法是:先把清單(通常是一個 ListView)寫在版面裡並隱藏起來,然後監聽 EditText 的焦點變化。

版面配置

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

程式碼

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            listView.setVisibility(View.VISIBLE);
        } else {
            listView.setVisibility(View.GONE);
        }
    }
});

六、更好的做法:AutoCompleteTextView

上面這個焦點切換的寫法有兩個缺點:清單不會隨著輸入內容過濾,而且使用者點擊清單以外的地方時清單不會消失(因為失焦不一定發生)。Android 本身提供了專門的元件:

<AutoCompleteTextView
    android:id="@+id/autoComplete"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:completionThreshold="1" />
ArrayAdapter<String> adapter = new ArrayAdapter<>(
        this, android.R.layout.simple_list_item_1, suggestions);
autoComplete.setAdapter(adapter);

completionThreshold 決定輸入幾個字之後才顯示建議,預設是 2。它會自動處理過濾、顯示、點選填入與隱藏,比自己寫監聽器省事得多。

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