java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.AdapterView<T extends android.widget.Adapter>
↳ android.widget.AbsSpinner
↳ android.widget.Spinner
android.widget.Spinner继承了android.view.ViewGroup类。下面安排一个案例如图6-5,有两个Spinner,颜色选择Spinner和星座选择Spinner。点击颜色选择Spinner右边的 按钮,如图6-6所示,弹出单选对话框。
![](http://dl.iteye.com/upload/attachment/513398/3fae674d-9e47-3990-a41c-25ed2c662eed.png)
图6-5 Spinner控件 图6-6 颜色选择Spinner
点击星座选择Spinner右边的 按钮,如图6-7所示,弹出列表对话框。
![](http://dl.iteye.com/upload/attachment/513400/5e6a8cfc-0b7e-312f-949b-03216c2ac91a.png)
图6-7 星座选择Spinner
程序代码请参考代码清单6-3:
【代码清单6-3】 chapter6_2/src/com/work/SpinnerActivity.java
public class SpinnerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView txt2 = (TextView) findViewById(R.id.TextView02);
Spinner s1 = (Spinner) findViewById(R.id.Spinner01);
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.colors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
txt2.setText(adapter.getItem(position).toString());
}
public void onNothingSelected(AdapterView<?> parent) {
txt2.setText("没选中的");
}
});
final TextView txt4 = (TextView) findViewById(R.id.TextView04);
Spinner s2 = (Spinner) findViewById(R.id.Spinner02);
ArrayAdapter<CharSequence> adapterC = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item, CONSTELLATIONS);
adapterC.setDropDownViewResource(android.R.layout.simple_spinner_item);
s2.setAdapter(adapterC);
s2.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
txt4.setText(CONSTELLATIONS[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
txt4.setText("没选中的");
}
});
}
static final String[] CONSTELLATIONS = new String[] {
"白羊座", "金牛座", "双子座",
"巨蟹座", "狮子座", "处女座",
"天枰座", "天蝎座", "射手座",
"魔蝎座", "水瓶座", "双鱼座"
};
}
第一个颜色选择Spinner数据来源于Values文件夹内的arrays.xml文件,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="colors">
<item>红色</item>
<item>橙色</item>
<item>黄色</item>
<item>绿色</item>
<item>蓝色</item>
<item>紫色</item>
</string-array>
</resources>
通过下面的方法从一个资源XML文件中加载数据,创建一个数组类的适配器对象,布局文件是使用系统提供的layout.simple_spinner_item,这里布局文件设置的是Spinner布局样式。
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.colors, android.R.layout.simple_spinner_item);
而弹出的下拉列表的样式是由Spinner的适配器的setDropDownViewResource()方法设置的。
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
第二个Spinner用于星座选择,数据是用数组存储的。这两个Spinner的setOnItemSelectedListener()方法的事件处理如下:
s1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
txt2.setText(adapter.getItem(position).toString());
}
public void onNothingSelected(AdapterView<?> parent) {
txt2.setText("没选中的");
}
});
其中的onItemSelected(AdapterView<?> parent, View view, int position, long id)方法是选中选择项目时触发的,其中的position选择项目的位置,id是选择项目的编号。而什么都不选的方法是onNothingSelected(AdapterView<?> parent)。
出自《Android开发案例驱动教程》第六章