Android Spinner List selection box

Source: Internet
Author: User

Spinner

Spinner is a drop-down list that is typically used to select a range of selectable list items, which can use an adapter or directly set the array source.

1. Set the array source directly

Set the array source in Res/values/strings.xml

1 <String-arrayname= "selector">2         <Item>Student ID</Item>3         <Item>Marriage certificate</Item>4         <Item>Officer Badge</Item>5         <Item>Member Card</Item>6     </String-array>

Then find spinner in Mainactivity and set the default selection

1 Importandroid.app.Activity;2 ImportAndroid.os.Bundle;3 ImportAndroid.widget.Spinner;4 5  Public classMainactivityextendsActivity {6     /**7 * Spinner list selection box8 * is a drop-down list, typically used to select a range of selectable list items, which can use an adapter or directly set the array source9      */Ten @Override One     protected voidonCreate (Bundle savedinstancestate) { A         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -          theSpinner Spinner =(Spinner) Findviewbyid (r.id.spinner); -          -        spinner.setselection (2, true);//starting from 0 -     } +  -      +}

There is a big difference between the setselection (int position, Boolean animate) and setselection (int position) implementations here, when the former is called for re-layout, Immediately triggers the onitemselected function, which acts as a direct click by hand. And the latter set the next choice location: setnextselectedpositionint (position); Then request layout, and Requestlayout is not executed immediately, just a schedule. However, the latter may lose some state when reloading the data and then layout.

Run effect

2, use the adapter, and set the monitoring

 Public classMainactivityextendsActivity {/*** Spinner List selection box * is a drop-down list that is typically used to select a range of selectable list items, which can use an adapter or directly set the array source*/        //Data Sourcestring[] data = {"Mom", "Papa", "Grandpa", "Grandma", "Grandpa", "grandmother"}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Spinner Spinner=(Spinner) Findviewbyid (R.id.spinner); //directly using the array source//spinner.setselection (2,true); //using Adaptersarrayadapter<string> adapter =NewArrayadapter<string> (mainactivity. This, Android.        R.layout.simple_list_item_1, data);                Spinner.setadapter (adapter); //Drop -down list selection box, set listenerSpinner.setonitemselectedlistener (NewOnitemselectedlistener () {@Override Public voidOnitemselected (adapterview<?>Parent, view view,intPositionLongID) {//Direct Array Source//String name = Getresources (). Getstringarray (R.array.selector) [position]; //Toast.maketext (Getbasecontext (), name, Toast.length_short). Show (); //adapter.String name =Data[position]; //in fact, if it is mainactivity.this can, but if changed to this, will be an error. Because this uses an anonymous inner class.Toast.maketext (Getbasecontext (), name, Toast.length_short). Show (); } @Override Public voidOnnothingselected (adapterview<?>parent) {                //when there is no choice, the call never fires.LOG.E ("TAG", "never triggered");    }        }); }    }

Run effect

Android Spinner List selection box

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.