入力方法のタイプを指定する

Compose をお試しください
Jetpack Compose は、Android で推奨される UI ツールキットです。Compose でタッチと入力を使用する方法をご覧ください。

テキスト フィールドはすべて、メールアドレス、電話番号、またはプレーン テキストなど、特定の種類のテキスト入力を想定しています。システムが適切なソフト入力方法(画面キーボードなど)を表示するように、アプリの各テキスト フィールドに入力タイプを指定する必要があります。

入力方法で使用できるボタンのタイプ以外にも、入力方法でスペルの候補を提供するかどうか、新しい文を大文字にするかどうか、改行ボタンを [完了] または [次へ] などの操作ボタンに置き換えるかどうかなどの動作を指定できます。このページでは、これらの特性を指定する方法について説明します。

キーボード タイプの指定

常にテキスト フィールドの入力方法を宣言するには、 android:inputType 属性を <EditText>要素に追加します。

電話番号を入力
図 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 属性の有効な値はドキュメントに記載されており、一部の値を組み合わせて入力メソッドの外観と追加の動作を指定できます。

スペルの候補と他の動作の有効化

自動修正
図 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"
    ... />

入力方法のアクションの指定

ほとんどのソフト入力方法では、現在のテキスト フィールドに適したユーザー アクション ボタンが下隅にあります。デフォルトでは、テキスト フィールドで複数行テキストが(android:inputType="textMultiLine" などで)サポートされている場合を除き、システムはこのボタンを [次へ] または [完了] アクションに使用します。複数行テキストがサポートされている場合、アクション ボタンは改行です。ただし、[送信] または [移動] など、テキスト フィールドにより適した別のアクションを指定できます。

キーボードの操作ボタンを指定するには、 android:imeOptions 属性を使用して "actionSend" または "actionSearch" などのアクション値を指定します。次に例を示します。

送信ボタン
図 4.[Send] ボタンは、 android:imeOptions="actionSend" を宣言すると表示されます。
<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;
    }
});

オートコンプリート候補の提供

入力時に候補を提供する場合は、AutoCompleteTextView という EditText のサブクラスを使用できます。 オートコンプリートを実装するには、テキストの候補を提供する Adapter を指定する必要があります。データの送信元(データベースやアレイなど)に応じて、数種類のアダプターを使用できます。

テキスト候補
図 5.テキスト 候補を表示する AutoCompleteTextView の例。

次の手順では、ArrayAdapter を使用して、配列から候補を提供する AutoCompleteTextView を設定する方法について説明します。

  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. Activity または Fragment で、次のコードを使用して、候補を提供するアダプターを指定します。

    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 に割り当てます。