指定輸入法類型

試試 Compose 的方式
Jetpack Compose 是 Android 推薦的 UI 工具包。瞭解如何在 Compose 中使用觸控和輸入功能。

每個文字欄位都會要求特定類型的文字輸入內容,例如電子郵件地址、電話號碼或純文字。您必須為應用程式中的每個文字欄位指定輸入類型,讓系統顯示適當的軟體輸入法,例如螢幕鍵盤。

除了輸入法可用的按鈕類型之外,您還可以指定輸入法提供拼字建議、將新句子轉為大寫,以及將列車回車按鈕替換為「完成」或「下一個」等動作按鈕的行為。本頁面將說明如何指定這些特性。

指定鍵盤類型

請務必在 <EditText> 元素中加入 android:inputType 屬性,宣告文字欄位的輸入方式。

電話號碼輸入
圖 1. phone 輸入類型。

舉例來說,如果您想要使用輸入法輸入電話號碼,請使用 "phone" 值:

<EditText
    android:id="@+id/phone"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/phone_hint"
    android:inputType="phone" />
textPassword 輸入類型
圖 2. textPassword 輸入類型。

如果文字欄位是用於密碼,請使用 "textPassword" 值,讓文字欄位隱藏使用者輸入的內容:

<EditText
    android:id="@+id/password"
    android:hint="@string/password_hint"
    android:inputType="textPassword"
    ... />

android:inputType 屬性會記錄幾個可能的值,您可以結合部分值來指定輸入法外觀和其他行為。

啟用拼字建議和其他行為

autocorrect
圖 3. 加入 textAutoCorrect 可自動更正拼錯。

android:inputType 屬性可讓您為輸入方法指定各種行為。最重要的是,如果文字欄位是用於基本文字輸入 (例如文字訊息),請使用 "textAutoCorrect" 值啟用自動拼字校正功能。

您可以使用 android:inputType 屬性結合不同的行為和輸入法樣式。舉例來說,以下是如何建立文字欄位,讓系統將句子的第一個字母改為大寫,並自動修正拼錯:

<EditText
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType=
        "textCapSentences|textAutoCorrect"
    ... />

指定輸入法動作

大多數軟體輸入法會在底角提供使用者動作按鈕,以便使用者操作目前的文字欄位。根據預設,系統會使用這個按鈕執行「Next」或「Done」動作,除非文字欄位支援多行文字 (例如使用 android:inputType="textMultiLine"),在這種情況下,動作按鈕會是換行符號。不過,您可以指定其他動作,這些動作可能更適合您的文字欄位,例如「傳送」或「前往」

如要指定鍵盤動作按鈕,請使用 android:imeOptions 屬性,並搭配 "actionSend""actionSearch" 等動作值。例如:

傳送按鈕
圖 4. 宣告 android:imeOptions="actionSend" 時,系統會顯示「Send」按鈕。
<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSend" />

接著,您可以為 EditText 元素定義 TextView.OnEditorActionListener,監聽動作按鈕的按下事件。在事件監聽器中,回應 EditorInfo 類別中定義的適當 IME 動作 ID,例如 IME_ACTION_SEND,如以下範例所示:

Kotlin

findViewById<EditText>(R.id.search).setOnEditorActionListener { v, actionId, event ->
    return@setOnEditorActionListener when (actionId) {
        EditorInfo.IME_ACTION_SEND -> {
            sendMessage()
            true
        }
        else -> false
    }
}

Java

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});

提供自動完成建議

如果您想在使用者輸入時提供建議,可以使用名為 AutoCompleteTextViewEditText 子類別。如要實作自動完成功能,您必須指定提供文字建議的 Adapter。可用的轉換器有幾種,取決於資料來源,例如資料庫或陣列。

文字建議
圖 5. AutoCompleteTextView 包含文字建議的範例。

以下程序說明如何設定 AutoCompleteTextView,以便使用 ArrayAdapter 提供陣列中的建議:

  1. AutoCompleteTextView 新增至版面配置。以下是僅包含文字欄位的版面配置:
    <?xml version="1.0" encoding="utf-8"?>
    <AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/autocomplete_country"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
  2. 定義包含所有文字建議的陣列。例如,以下是國家/地區名稱陣列:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="countries_array">
            <item>Afghanistan</item>
            <item>Albania</item>
            <item>Algeria</item>
            <item>American Samoa</item>
            <item>Andorra</item>
            <item>Angola</item>
            <item>Anguilla</item>
            <item>Antarctica</item>
            ...
        </string-array>
    </resources>
  3. ActivityFragment 中,使用下列程式碼指定提供建議的轉接程式:

    Kotlin

    // Get a reference to the AutoCompleteTextView in the layout.
    val textView = findViewById(R.id.autocomplete_country) as AutoCompleteTextView
    // Get the string array.
    val countries: Array<out String> = resources.getStringArray(R.array.countries_array)
    // Create the adapter and set it to the AutoCompleteTextView.
    ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries).also { adapter ->
        textView.setAdapter(adapter)
    }

    Java

    // Get a reference to the AutoCompleteTextView in the layout.
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    // Get the string array.
    String[] countries = getResources().getStringArray(R.array.countries_array);
    // Create the adapter and set it to the AutoCompleteTextView.
    ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
    textView.setAdapter(adapter);

    在上例中,系統會初始化新的 ArrayAdapter,將 countries_array 字串陣列中的每個項目繫結至 simple_list_item_1 版面配置中存在的 TextView。這是 Android 提供的版面配置,可為清單中的文字提供標準外觀。

  4. 呼叫 setAdapter() 將轉接程式指派給 AutoCompleteTextView