.xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Spinner android:id="@+id/city" android:prompt="@string/city_prompt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/city_lables"/> <Spinner android:id="@+id/area" android:prompt="@string/city_prompt" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>.java 代码如下:
package org.lxh.demo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; public class MySpinnerListenerDemo extends Activity { private Spinner city = null; // 下拉列表框内容 private Spinner area = null; // 二级列表 private String[][] areaData = new String[][] { { "东城", "西城", "朝阳", "大兴", "平谷" }, // 针对于一级的子信息 { "黄浦", "杨浦", "闵行" }, // 针对于二级的子信息 { "广州" } // 针对于三级的子信息 }; // 子菜单项 private ArrayAdapter<CharSequence> adapterArea = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); this.city = (Spinner) super.findViewById(R.id.city); // 取得下拉列表框 this.area = (Spinner) super.findViewById(R.id.area); // 取得下拉列表框 this.city.setOnItemSelectedListener(new OnItemSelectedListenerImpl()); } private class OnItemSelectedListenerImpl implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, 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.area .setAdapter(MySpinnerListenerDemo.this.adapterArea);// 设置二级下拉列表的选项内容 } public void onNothingSelected(AdapterView<?> arg0) { // 表示没有选项的时候触发 // 一般此方法现在不关心 } } }
源码下载:http://download.csdn.net/detail/yayun0516/8363843
原文地址:http://blog.csdn.net/yayun0516/article/details/42739015