Spinner (drop-down list box)
|
Method |
Describe |
|
Charsequence getprompt () |
Get hint text |
|
void Setprompt (Charsequence prompt) |
Set the ToolTip text for a component |
|
void Setadapter () |
Set drop-down list box |
|
Void Setonitemclicklistener
(Adapterview.onitemclicklistener L) |
Set Options Click events |
How drop-down lists are configured
A. resource file configuration
First step: Configure in String.xml
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <String-arrayname= "Pro"> <Item>"Hunan Province"</Item> <Item>"Guangdong Province"</Item> <Item>"Sichuan Province"</Item> <Item>"Jiangsu Province"</Item> </String-array></Resources>
String.xml
B. Adapter-specific resources
The first type: resource configuration
arrayadapter<charsequence> adapter =
Arrayadapter.createfromresource (this, resource ID, List-displayed style);
1 Public classSpinneradapterextendsActivity {2 @Override3 Public voidonCreate (Bundle savedinstancestate) {4 Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.main);6 7 //define an adapter to read the contents of a province in an XML file8arrayadapter<charsequence> adapter =Arrayadapter.createfromresource (9 This, R.array.pro, Android. R.layout.simple_spinner_item);Ten //set list display style Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item); One A //Remove the drop-down box from the XML file - FinalSpinner spin = (Spinner)Super. Findviewbyid (R.id.spinner01); -Spin.setadapter (adapter);//The adapter object for the province is passed in theSpin.setprompt ("Select Province"); - } -}Resource Configuration
Second type: List configuration
list< charsequence > List=new arraylist< charsequence > ();
List. Add ();
Arrayadapter <CharSequence> adapter =
Arrayadapter.createfromresource (this, list display style, collection data [list]);
//CollectionList<charsequence> list=NewArraylist<charsequence>(); List.add ("Hunan Province"); List.add ("Guangdong province"); List.add ("Sichuan Province");//define an adapter to read the contents of a province in an XML filearrayadapter<charsequence> adapter = Arrayadapter.createfromresource ( This, Android. r.layout.simple_spinner_item,list);//Set the list display styleAdapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);//first drop -down box FinalSpinner spin = (Spinner)Super. Findviewbyid (R.id.spinner01); Spin.setadapter (adapter); //The adapter object for the province is passed inSpin.setprompt ("Select Province"); list configurationList Configuration
"Set list display style"
Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);
Drop-down list box "Android 3"