这里首先介绍一下Spinner、AutoCompleteTextView、MultiAutoCompleteTextView。
Spinner是一个下拉列表,点击时会自动弹出选项,选项的元素来自适配器。
AutoCompleteTextView自动完成文本框继承自EditText,可以根据用户输入的文本弹出一个智能提示的列表,就如百度、谷歌等的搜索引擎。
MultiAutoCompleteTextView继承了AutoCompleteTextView,它的功能和AutoCompleteTextView差不多,是以分词器分隔键入多个智能提示字串。
这三种控件的共同点都是弹出列表,且列表中的元素都是通过适配器来管理。
main.xml (通过线性布局定义了三中控件的属性,并嵌套了一个滚动条)
<?xml version="1.0" encoding="utf-8"?>
<!-- 嵌套一个滚动条 -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/android"
>
<TextView
android:text="爱好"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="@color/red"
/>
<Spinner
android:id="@+id/Spinner"
android:layout_width="250dip"
android:layout_height="wrap_content"
android:layout_marginBottom="50dip"
/>
<TextView
android:text="AutoCompleteTextView"
android:id="@+id/TextView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="@color/red"
/>
<AutoCompleteTextView
android:id="@+id/actv"
android:layout_width="250dip"
android:layout_height="wrap_content"
android:layout_marginBottom="50dip"
/>
<TextView
android:text="MultiAutoCompleteTextView"
android:id="@+id/TextView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="@color/red"
/>
<MultiAutoCompleteTextView android:id="@+id/mactv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
然后在java代码中定义控件所需数据的数组
//Spinner下拉列表的内容数组
private static final String[] ball={"足球","篮球","乒乓球"};
//AutoCompleteTextView和MultiAutoCompleteTextView自动完成控件的所需数据的数据源
private static final String[] actv=new String[]{"周杰伦","周公","周迅",
"周立波","周润发","周星驰","周恩来","林书豪","林心如","林俊杰","林志颖"};
以下是Spinner控件的实现方法
//获取Spinner ID
Spinner sp = (Spinner) findViewById(R.id.Spinner);
//将可选内容ball字符数组与ArrayAdapter连接起来
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ball);//设置下拉列表的风格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将adapter 添加到spinner中
sp.setAdapter(adapter);
//添加事件Spinner事件监听
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView) findViewById(R.id.TextView);
//通过文本框显示数据 tv.setText("爱好:"+ball[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//以下是AutoCompleteTextView 实现方法
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.actv);//定义AutoCompleteTextView控件
ArrayAdapter<String> Autoadapter=new ArrayAdapter<String>(this, //定义匹配源的adapter
android.R.layout.simple_dropdown_item_1line, actv);
textView.setAdapter(Autoadapter); //设置 匹配源的adapter 到 AutoCompleteTextView控件
textView.setThreshold(1); //设置输入多少字符时自动匹配
//以下是MultiAutoCompleteTextView 实现方法
MultiAutoCompleteTextView mactv= (MultiAutoCompleteTextView)findViewById(R.id.mactv);
mactv.setAdapter(Autoadapter); //设配器
mactv.setThreshold(1); ///设置 匹配源的adapter 到 MultiAutoCompleteTextView控件
//用户必须提供一个MultiAutoCompleteTextView.Tokenizer分词器用来分隔子串。逗号是分隔符
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
最后贴出三张图 和 源代码下载地址!!!
源代码下载地址:http://download.csdn.net/detail/liaozishen/4107015