1,布局文件中定义AutoCompleteTextView组件
2,Activity中创建对布局文件中AutoCompleteTextView的引用
3,创建ArrayAdapter对象作为AutoCompleteTextView对象的资源文件
4,调用AutoCompleteTextView的setAdapter()方法
具体代码如下:
package com.sun.auto_complete_text_view_test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutoCompleteTextViewTestActivity extends Activity {
/** Called when the activity is first created. */
AutoCompleteTextView autoCompleteTextView = null;
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.auto_complete_text_view);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
autoCompleteTextView.setAdapter(adapter);
}
}