In the development we often encounter similar spinner cascading menu effect, the following is the implementation of spinner cascading menu effect
Activity_main.xml
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Fill_parent"4android:layout_height= "Fill_parent"5android:orientation= "Vertical" >6 7<LinearLayout8Android:layout_width= "Fill_parent"9android:layout_height= "Wrap_content"Tenandroid:orientation= "Horizontal" > One A<!--android:text= "@string/city_prompt" information-- -<!--the contents of the android:entries= "@array/city_labels" drop-down list-- - the -<!--first-class columns-- - -<Spinner +Android:id= "@+id/city" -Android:layout_width= "Wrap_content" +android:layout_height= "Wrap_content" Aandroid:entries= "@array/city_labels" atandroid:text= "@string/city_prompt"/> -<!--level two columns-- - -<Spinner -Android:id= "@+id/area" -Android:layout_width= "Wrap_content" inandroid:layout_height= "Wrap_content" -android:text= "@string/area_prompt"/> to</LinearLayout> + -<LinearLayout theAndroid:layout_width= "Fill_parent" *android:layout_height= "Wrap_content" $android:orientation= "Horizontal" >Panax Notoginseng -<TextView theAndroid:id= "@+id/tex_city" +Android:layout_width= "Wrap_content" Aandroid:layout_height= "Wrap_content" theandroid:text= "City"/> + -<TextView $Android:id= "@+id/tex_area" $Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -android:text= "Area" the/> -</LinearLayout>Wuyi the</LinearLayout>
Array.xml
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <resources> 3 <string-array Name= "City_labels" > 4 <item> Please select:</item> 5 <item> Beijing </item> 6 <item> Shanghai </item> 7 <item> Wuhan </item> 8 </string-array > 9 </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" > drop-down list box options </string> 8 <string Name= "City_prompt" > Choose your preferred City </string> 9 <string name= "area_prompt" > Choose your favorite Town </ string> </resources>
Activity
1 PackageCom.example.spinnerji;2 Importandroid.app.Activity;3 ImportAndroid.os.Bundle;4 ImportAndroid.view.View;5 ImportAndroid.widget.AdapterView;6 ImportAndroid.widget.AdapterView.OnItemSelectedListener;7 ImportAndroid.widget.ArrayAdapter;8 ImportAndroid.widget.Spinner;9 ImportAndroid.widget.TextView;Ten One Public classMainactivityextendsActivity { A PrivateSpinner City;//level Menu - PrivateSpinner area;//Level Two menu - PrivateTextView Citytextview; the PrivateTextView Aretextview; - Privatestring[][] Areadata =Newstring[][] { -{"Please select:"}, -{"Chaoyang", "Haidian", "Xicheng", "Daxing District", "Huairou"}, +{"Minhang"}, -{"Hongshan District", "Wuchang District", "Dongxihu District"}}; + PrivateArrayadapter<charsequence>Adapterarea; A at @Override - Public voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -citytextview=(TextView) Findviewbyid (r.id.tex_city); -aretextview=(TextView) Findviewbyid (R.id.tex_area); inCity = (Spinner) Findviewbyid (r.id.city);//get drop-down list box - //since there are multiple urban areas in a city, for the sake of simplicity, we define an array, save several cities through an array, and actually require separate data toArea = (Spinner) Findviewbyid (R.id.area);//get drop-down list box +City.setonitemselectedlistener (NewOnitemselectedlistenerimpl ()); -Area.setonitemselectedlistener (NewOnitemselectedlistenerimpl ()); theCity.setprompt ("Choose the city You like"); *City.setprompt ("Choose the city You like"); $ }Panax Notoginseng - Private classOnitemselectedlistenerimplImplementsOnitemselectedlistener { the Public voidOnitemselected (adapterview<?>Parent, view view, + intPositionLongID) {//trigger when option is changed A the Switch(Parent.getid ()) { + Caser.id.city: -String value = (string) parent.getitematposition (position);//get the selected option $ Citytextview.settext (value); $Adapterarea =NewArrayadapter<charsequence> (mainactivity. ThisAndroid. R.layout.simple_expandable_list_item_1,//Display Style -Areadata[position]);//The object that is represented in the list view. -Area.setadapter (Adapterarea);//add Adapterarea to area the Break; - Wuyi CaseR.id.area: theString value2 = (string) parent.getitematposition (position);//get the selected option - Aretextview.settext (value2); Wu Break; - } About $ } - - Public voidonnothingselected (adapterview<?> arg0) {//trigger when no options are in the - //generally do not care A + } the - } $}
Android Spinner Cascading Menu implementation