今回はAndroidアプリでオートコンプリートを実現する方法を紹介する。
オートコンプリート機能を実現するにはAutoCompleteTextViewを使う。
下のサンプルはMonth(月)を自動補完するテキストボックスの例だ。
まずはメッセージリソース。
デフォルトのまま、特に変更は無し。
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">AutoCompleteSample</string> <string name="action_settings">Settings</string> </resources>
次に画面レイアウト。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Month:" /> <AutoCompleteTextView android:id="@+id/autocomplete_month" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:inputType="text" /> </LinearLayout>AutoCompleteTextView の android:inputType="text" で改行禁止にしている。
次に「補完候補リスト」TextViewのUIを定義する。
このXMLファイルを配置する場所はとりあえずactivity_main.xmlと同じ階層に。
list_months.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textColor="#000" android:textSize="16sp" > </TextView>
次にアクティビティ。
ActivityMain.java
package com.example.autocompletesample; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class MainActivity extends Activity { static final String[] MONTHS = new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * AutoCompleteTextView基本設定 */ // インスタンスを生成して AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_month); // アダプタを登録 ArrayAdapteradapter = new ArrayAdapter (this, R.layout.list_months, MONTHS); textView.setAdapter(adapter); /** * ここからオプション設定 */ // ドロップダウンリスト最下段に表示されるヒント textView.setCompletionHint("Choose Months."); // オートコンプリート開始までの文字数(0以下は指定できない) textView.setThreshold(1); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
最も重要なポイントは2つ。
ポイント1つ目。
static final String[] MONTHS = new String[] {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
これで静的なMonthリストを定義している。
次にポイント2つ目。
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_month);
ArrayAdapteradapter = new ArrayAdapter (this, R.layout.list_months, MONTHS);
textView.setAdapter(adapter);
AutoCompleteTextViewのインスタンスを生成し、Month補完候補リストの配列アダプタを生成し、AutoCompleteTextViewにその配列アダプタをセット。
補完候補リストを可変にしたい場合は、その都度ArrayAdapterコンストラクタの3番目の引数に利用したい配列をセットしてアダプタのインスタンスを生成し、textView.setAdapter()すれば良い。
■参考にさせていただいたサイト
AutoCompleteを使う: TechBooster