1}AutoCompleteTextView
AutoCompleteTextView效果
1)在布局文件中声明一个AutoCompleteTextView
在res/layout下新建一个布局文件,名为list_item.xml
autoCompleteTextView.setAdapter(arrayAdapter);
1)在布局文件中声明一个AutoCompleteTextView
<AutoCompleteTextView android:id="@+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
2)定义提示条目的样式
在res/layout下新建一个布局文件,名为list_item.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:textSize="16sp"
android:textColor="#000">
</TextView>
3)创建一个ArrayAdapter来提供数据
//创建一个list,为ArrayAdapter提供数据
List<String> list = new ArrayList<String>();
list.add("测试测试");
list.add("测试test");
//创建一个ArrayAdapter对象
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,list);
4)为
AutoCompleteTextView设置数据
autoCompleteTextView.setAdapter(arrayAdapter);
5)代码
public class MainActivity extends Activity {
/** Called when the activity is first created. */
AutoCompleteTextView autoCompleteTextView = null;
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
"Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
"Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过ID得到AutoCompleteTextView对象
autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autocomplete);
//创建一个list,为ArrayAdapter提供数据
List<String> list = new ArrayList<String>();
list.add("测试测试");
list.add("测试test");
//创建一个ArrayAdapter对象
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,list);
// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,COUNTRIES);
//将ArrayAdapter设置给AutoCompleteTextView对象 autoCompleteTextView.setAdapter(arrayAdapter); }
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.list_item,COUNTRIES); 这个既可以传list对象,也可以设置为一个String数组.Android测试时要输入两个字符才会有提示.
2} MultiAutoCompleteTextView
1.布局文件
<MultiAutoCompleteTextView android:id="@+id/mul"
android:layout_width="fill_parent" android:layout_height="wrap_content"></MultiAutoCompleteTextView>
2. MainActivity
mul = (MultiAutoCompleteTextView) this.findViewById(R.id.mul);
mul.setAdapter(adapter);
mul.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
// 完成对选项的拆分的功能,以逗号进行拆分
MultiAutoCompleteTextView 可以实现多个自动提示。