首页 > 代码库 > Android Spinner级联菜单实现

Android Spinner级联菜单实现

在开发中我们经常要遇到类似Spinner级联菜单的效果,下面就是Spinner级联菜单效果的实现

activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent" 5     android:orientation="vertical" > 6  7     <LinearLayout 8         android:layout_width="fill_parent" 9         android:layout_height="wrap_content"10         android:orientation="horizontal" >11 12         <!-- android:text="@string/city_prompt" 提示信息 -->13         <!-- android:entries="@array/city_labels"下拉列表的内容 -->14 15 16         <!-- 一级栏目 -->17 18         <Spinner19             android:id="@+id/city"20             android:layout_width="wrap_content"21             android:layout_height="wrap_content"22             android:entries="@array/city_labels"23             android:text="@string/city_prompt" />24         <!-- 二级栏目 -->25 26         <Spinner27             android:id="@+id/area"28             android:layout_width="wrap_content"29             android:layout_height="wrap_content"30             android:text="@string/area_prompt" />31     </LinearLayout>32 33     <LinearLayout34         android:layout_width="fill_parent"35         android:layout_height="wrap_content"36         android:orientation="horizontal" >37 38         <TextView39             android:id="@+id/tex_city"40             android:layout_width="wrap_content"41             android:layout_height="wrap_content"42             android:text="city" />43 44         <TextView45             android:id="@+id/tex_area"46             android:layout_width="wrap_content"47             android:layout_height="wrap_content" 48             android:text="area"49             />50     </LinearLayout>51 52 </LinearLayout>

 

array.xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3      <string-array name="city_labels"> 4         <item>请选择:</item> 5           <item>北京</item> 6             <item>上海</item> 7                 <item>武汉</item> 8     </string-array> 9    10 </resources>

 

strings.xml

 1 <resources> 2  3     <string name="app_name">SpinnerJi</string> 4     <string name="hello_world">Hello world!</string> 5     <string name="menu_settings">Settings</string> 6     <string name="title_activity_main">MainActivity</string> 7     <string name="city_app">下拉列表框选项</string> 8     <string name="city_prompt">选择你喜欢的城市</string> 9     <string name="area_prompt">选择你喜欢的城区</string>10 11 </resources>

 

activity

 1 package com.example.spinnerji; 2 import android.app.Activity; 3 import android.os.Bundle; 4 import android.view.View; 5 import android.widget.AdapterView; 6 import android.widget.AdapterView.OnItemSelectedListener; 7 import android.widget.ArrayAdapter; 8 import android.widget.Spinner; 9 import android.widget.TextView;10 11 public class MainActivity extends Activity {12     private Spinner city;// 一级菜单13     private Spinner area;// 二级菜单14     private TextView cityTextView;15     private TextView areTextView;16     private String[][] areaData = http://www.mamicode.com/new String[][] {17             {"请选择:"},18             {"朝阳区","海淀区","西城区","大兴区","怀柔区"},19             {"闵行区"},20             {"洪山区","武昌区","东西湖区"}};21     private ArrayAdapter<CharSequence> adapterArea;22 23     @Override24     public void onCreate(Bundle savedInstanceState) {25         super.onCreate(savedInstanceState);26         setContentView(R.layout.activity_main);27         cityTextView=(TextView) findViewById(R.id.tex_city);28         areTextView=(TextView) findViewById(R.id.tex_area);29         city = (Spinner) findViewById(R.id.city);// 取得下拉列表框30         // 由于一个城市有多个市区,为了简单起见下面我们定义一个数组,通过数组保存若干个城市,而实际中需要单独的数据31         area = (Spinner) findViewById(R.id.area);// 取得下拉列表框32         city.setOnItemSelectedListener(new OnItemSelectedListenerImpl());33         area.setOnItemSelectedListener(new OnItemSelectedListenerImpl());34         city.setPrompt("选择你喜欢的城市");35         city.setPrompt("选择你喜欢的城区");36     }37 38     private class OnItemSelectedListenerImpl implements OnItemSelectedListener {39         public void onItemSelected(AdapterView<?> parent, View view,40                 int position, long id) {// 选项改变的时候触发41             42             switch (parent.getId()) {43             case R.id.city:44                 String value = http://www.mamicode.com/(String) parent.getItemAtPosition(position);// 得到选中的选项45                 cityTextView.setText(value);46                 adapterArea = new ArrayAdapter<CharSequence>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,// 显示风格47                         areaData[position]);// 在列表视图中所代表的对象。48                 area.setAdapter(adapterArea);// 把adapterArea添加到area49                 break;50 51             case R.id.area:52                 String value2 = (String) parent.getItemAtPosition(position);// 得到选中的选项53                 areTextView.setText(value2);54                 break;55             }56             57         }58 59         public void onNothingSelected(AdapterView<?> arg0) {// 没有选项的时候触发60             // 一般不关心61 62         }63 64     }65 }

 

Android Spinner级联菜单实现