入力サジェスト

入力サジェスト

入力サジェストを使うには、
AutoCompleteTextViewを使う

あらかじめサジェスト候補の文字列をセットしておくことで
該当する文字を途中まで入力すると候補がでてくるので便利

携帯とかの文字予測みたいなかんじ

AutoCompleteTextViewは、サジェスト機能が使えるTextView

使い勝手は、TextViewと同じ

まず、レイアウトファイルで
AutoCompleteTextViewを定義

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"/>

<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>

次に、ArrayAdapterクラスで
入力サジェストのAdapterを作成して
setAdapter()でセット

AutoCompleteTextView autoText = (AutoCompleteTextView)findViewById(R.id.autocomplete);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_line, androids);

autoComplete.setAdapter(adapter);

サジェスト入力の利用例としては
AutoCompleteTextViewは、
ArrayList型の
Adapterオブジェクトをセットするだけでサジェストを実現できるので
入力履歴のリストを利用すれば履歴をサジェストできるし
Google Suggest API
を使えば
Web検索候補を表示できるため
EditTextより使いやすいものを作れる

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です