The Chinese meaning of the spinner is the fine-tuning tool. In Android, it represents the drop-down list box. Its data source is adapter.
What elements are the most important for a drop-down box? First: data source 2: style displayed to the user 3: interaction with the user
Let's take a look at the sample code:
Package cn.com. chenzheng_java; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. hashmap; </P> <p> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br/> Import android. widget. adapterview; <br/> Import android. widget. arrayadapter; <br/> Import android. widget. baseadapter; <br/> import Droid. widget. spinner; <br/> Import android. widget. toast; <br/> Import android. widget. adapterview. onitemselectedlistener; </P> <p> public class spinneractivity extends activity {<br/> private spinner; <br/> string [] address = new string [] {<br/> "Haidian", <br/> "Haidian Huangzhuang", <br/> "Zhongguancun ", <br/> "Great Wall", <br/> "Yuan mingyuan" <br/> }; </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (S Avedinstancestate); <br/> setcontentview (R. layout. spinner); <br/> runwithtext (); </P> <p >}</P> <p>/** <br/> * the content of the drop-down box is the text spinner. <br/> */<br/> private void runwithtext () {<br/> spinner = (spinner) findviewbyid (R. id. spinner1); <br/>/** <br/> * New arrayadapter <string> (this, R. layout. the second parameter R. layout. spinner_text <br/> * must be a layout file with textview as the root label! For details, see the background code. The background code obtains a view based on the index and converts it to <br/> * textview type. <Br/> */<br/> arrayadapter <string> arrayadapter = new arrayadapter <string> (this, R. layout. spinner_text, address); <br/> // sets the pop-up drop-down box style for Android. r. layout. simple_spinner_dropdown_item is a style provided by Android. <br/> arrayadapter. setdropdownviewresource (Android. r. layout. simple_spinner_dropdown_item); </P> <p> spinner. setadapter (arrayadapter); <br/> adapterview. onitemselectedlistener listener = new onitemselectedliste NER () {</P> <p> @ override <br/> Public void onitemselected (adapterview <?> Parent, view, <br/> int position, long ID) {<br/> toast. maketext (spinneractivity. this, address [position], toast. length_long ). show (); </P> <p >}</P> <p>/** <br/> * parent is our spinner <br/> */<br/> @ override <br/> Public void onnothingselected (adapterview <?> Parent) {<br/> toast. maketext (spinneractivity. this, "Nothing is returned", toast. length_long ). show (); <br/>}< br/>}; </P> <p> spinner. setonitemselectedlistener (listener); </P> <p >}< br/>
--------------------------------------------------------------
In combination with the code, let's take a look at the three key elements mentioned above.
First, data source: we set the data source through arrayadapter.
Second layout style: You can use setdropdownviewresource () to add a layout file index for a specific style.
Third: interact with users. You can set event listening. The main implementation here is the setonitemselectedlistener () method. Triggered when different items are selected.
Running result of code on subsidy: