XML文件的代码
<AutoCompleteTextView android:id="@+id/antotext" android:layout_width="fill_parent" android:layout_height="wrap_content" />
java文件的代码
private static final String[] AUTO_CONTENT={"a","b","c","ab","bc","ca","abc","acb","bca","b","ab","ade","abcdf"};
private AutoCompleteTextView autoText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建ArrayAdapter对象
ArrayAdapter<String> auto_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, AUTO_CONTENT);
//获取AutoCompleteTextView对象
autoText=(AutoCompleteTextView) this.findViewById(R.id.antotext);
//将AutoCompleteTextView与ArrayAdapter进行绑定
autoText.setAdapter(auto_adapter);
}
运行结果图:
由上,AutoCompleteTextView 默认是输入二个字符自动提示。如果想设置输入第一个字符就进行提示,添加代码如下:
//设置输入第一个字符就进行提示
autoText.setThreshold(1);
运行结果图: