Android provides a very practical control spinner that implements the drop-down box feature.
The spinner control needs to add the spinner tag to the XML resource file, as follows:
<Spinner Android:id="@+id/spinner1"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_torightof="@+id/textview1"Android:layout_centerhorizontal="true"/> <TextView Android:id="@+id/textview1"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_alignbottom="@+id/spinner1"Android:layout_alignparentleft="true"Android:layout_marginbottom="20DP"Android:layout_marginleft="16DP"android:textsize="20SP"Android:text="Selected City"/>
After you add the resource control, in the activity, add the code:
PackageCom.example.android_spinner2;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.Spinner; Public classMainactivityextendsActivity {PrivateSpinner Spinner; PrivateArrayadapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); This. Spinner = (spinner) This. Findviewbyid (R.id.spinner1); //two ways to register an adapter with a drop-down list//1:new Adapter Object (Content object, style ID, data collection)adapter =NewArrayadapter<string> ( This, Android. R.layout.simple_spinner_dropdown_item, Getdatasource ()); Spinner.setadapter (adapter); } PublicList<string>Getdatasource () {List<String> list =NewArraylist<string>() ; List.add (Beijing); List.add (Shanghai); List.add (Guangzhou); List.add (Changsha); returnlist; } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
Running in the simulation will result in a spinner display:
From the official API, the official offers another way to register an adapter for Spiner. The sample code is as follows:
protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); This. Spinner = (spinner) This. Findviewbyid (R.id.spinner1); //two ways to register an adapter with a drop-down list//1:new Adapter Object (Content object, style ID, data collection)//adapter = new Arrayadapter<string> (This,//Android. R.layout.simple_spinner_dropdown_item, Getdatasource ()); //2:. Createfromresource (Content object, array ID in XML, style ID)adapter = Arrayadapter.createfromresource (mainactivity. This, R.array.planets_array,android. R.layout.simple_spinner_dropdown_item); Spinner.setadapter (adapter); }
Note that the second way to add an array to the string resource file is:
<string-array name= "Planets_array" > <item>Mercury</item> <item>venus</item > <item>Earth</item> </string-array
You can also get the list of drop-down boxes you want by running the project.
Android Development-spinner Implementation of the drop-down box control