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