Messy Android spinner Application

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.