?Spinner组件的主要功能是用于进行下拉列表显示的功能,当用户选中下拉列表中的某个选项之后可以使用Spinner类中提供的setOnItemClickListener()方法进行监听。
-
private Spinner city = null; // 定义下拉列表框
-
private Spinner area = null; // 定义下拉列表框
-
private String[][] areaData = new String[][] { // 定义联动菜单项
-
{ "东城", "西城", "朝阳", "大兴", "平谷" }, // 第一级子选项
-
{ "黄浦", "杨浦", "闵行" }, // 第二级子选项
-
{ "广州" } }; // 第三级子选项
-
private ArrayAdapter<CharSequence> adapterArea = null; // 下拉列表内容适配器
-
@Override
-
public void onCreate(Bundle savedInstanceState) {
-
this.city.setOnItemSelectedListener(
-
new OnItemSelectedListenerImpl()); // 设置监听器
-
}
-
private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
-
@Override
-
public void onItemSelected(AdapterView<?> adapterView, View view,
-
int position, long id) { // 选项选中时触发
-
MySpinnerListenerDemo.this.adapterArea = new ArrayAdapter<CharSequence>(
-
MySpinnerListenerDemo.this,
-
android.R.layout.simple_spinner_item,
-
MySpinnerListenerDemo.this.areaData[position]);
-
MySpinnerListenerDemo.this.adapterArea.setDropDownViewResource(
-
android.R.layout.simple_spinner_dropdown_item);
-
MySpinnerListenerDemo.this.area
-
.setAdapter(MySpinnerListenerDemo.this.adapterArea);
-
}
-
@Override
-
public void onNothingSelected(AdapterView<?> adapterView) { // 没有选项时触发
-
}
-
}
020604_下拉列表框与OnItemSelectedListener.ppt
阅读(1571) | 评论(0) | 转发(1) |