Android gives us a spinner control, which is basically a list, so let's say this control, this control has been seen in the past, but this is still a new introduction again. Spinner is located under the Android.widget package, displaying only the user-selected element at a time, and when the user taps again, a selection list pops up for the user to select, and the elements from the selection list are also from the adapter. Spinner is a subclass of the view class. Here is a concrete look at what's going on:
1.:
2.XML Code
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:id= "@+id/widget28"4 Android:layout_width= "Fill_parent"5 Android:layout_height= "Fill_parent"6 android:orientation= "vertical" >7 8 <TextView9 Android:id= "@+id/textview_show"Ten Android:layout_width= "Fill_parent" One Android:layout_height= "Wrap_content" A Android:text= "You chose to be" - android:textsize= "25SP" > - </TextView> the - <Spinner - Android:id= "@+id/spinner_city" - Android:layout_width= "Fill_parent" + Android:layout_height= "Wrap_content" > - </Spinner> + A </LinearLayout>
3.java Code
1 PackageCom.example.spinner;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 Importandroid.app.Activity;6 ImportAndroid.os.Bundle;7 ImportAndroid.view.View;8 ImportAndroid.widget.AdapterView;9 ImportAndroid.widget.ArrayAdapter;Ten ImportAndroid.widget.Spinner; One ImportAndroid.widget.TextView; A - Public classMainactivityextendsActivity { - the Privatelist<string> list =NewArraylist<string>(); - PrivateTextView Mytextview; - PrivateSpinner Myspinner; - PrivateArrayadapter<string>adapter; + - @Override + Public voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.activity_main); - Initspinner (); - } - - Private voidInitspinner () { - //First step: Add a list of drop-down list items, and the items you add here are the menu items for the drop-down list inList.add ("Beijing")); -List.add ("Shanghai"); toList.add ("Shenzhen"); +List.add ("Nanjing"); -List.add ("Chongqing"); theMytextview =(TextView) Findviewbyid (r.id.textview_show); *Myspinner =(Spinner) Findviewbyid (r.id.spinner_city); $ //Step Two: Define an adapter for the drop-down list, which is used in the list defined in the previous section. Panax Notoginsengadapter =NewArrayadapter<string> ( This, - Android. R.layout.simple_spinner_item, list); the //Step Three: Set the menu style for the adapter drop-down list. + Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item); A //Fourth Step: Add the adapter to the drop-down list the Myspinner.setadapter (adapter); + //Fifth Step: Set the response of various events for the drop-down list, the Response menu is selected - Myspinner $. Setonitemselectedlistener (NewSpinner.onitemselectedlistener () { $ Public voidOnitemselected (adapterview<?>arg0, View arg1, - intArg2,LongArg3) { - //TODO auto-generated Method Stub the /*bring the value of the selected Myspinner into the Mytextview*/ -Mytextview.settext ("You have selected:" +Adapter.getitem (arg2));Wuyi /*Display the Myspinner*/ the arg0.setvisibility (view.visible); - } Wu - Public voidOnnothingselected (adapterview<?>arg0) { About //TODO auto-generated Method Stub $ //mytextview.settext ("NONE"); - //arg0.setvisibility (view.visible); - } - }); A } +}
Android Learning-----Spinner