This blog post describes how to use the basic spinner
First write the Code section in XML
<spinner android:id= "@+id/myspinner" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" />
Write the file section in Strings.xml again.
<string-array name= "Tongxue" > <item> hou </item> <item> Zhang </ite M> <item> Zhao </item> <item> Lee </item> <item> Lee </item> <ite M> Liu </item> </string-array>
Finally, let's show you the Code section of the activity.
public class mainactivity extends activity {private spinner spin; @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedInstanceState); setcontentview (R.layout.activity_main); spin= (Spinner) Findviewbyid (R.id.myspinner); arrayadapter<charsequence> adapter=arrayadapter.createfromresource (this, R.array.tongxue, android. R.layout.simple_spinner_item); adapter.setdropdownviewresource ( Android. R.layout.simple_spinner_dropdown_item); spin.setadapter (Adapter ); spin.setprompt ("Test"); spin.setonitemselectedlistener (New spinnerlis ()); } //Define a listener class SpinnerLis implements onitemselectedlistener{@Overridepublic void onitemselected (adapterview<?> parent, view view,int position, long id) {string selected= Parent.getitematposition (position). ToString (); Toast.maketext (Getapplicationcontext (), selected, toast.length_long). Show (); @Overridepublic void onnothingselected (adapterview<?> parent) {} }
This article is from the "Java White Battlefield" blog, be sure to keep this source http://8023java.blog.51cto.com/10117207/1664415
Use of spinner in Android