Use of the Android:spinner (drop-down box) control
Create a paging file (Main.xml)
1 <spinner
2 android:id= "@+id/spinner1"
3 android:layout_width= "Match_parent"
4 android:layout_height= "Wrap_content"/>
5
6 <textview
7 android:id= "@+id/tvresult"
8 android:layout_width= "Match_parent"
9 android:layout_height= "Wrap_content"
Ten android:text= "Fruit of choice"/>
Create a data source for a drop-down box
1 list<string> List = new arraylist<string> ();
2 List.add ("Apple");
5 List.add ("banana");
7 List.add ("Orange");
9 List.add ("banana");
Create adapter (the data source for the drop-down box is from the adapter)
Arrayadapter<string> adapter=new arrayadapter<string> (this, Android. R.layout.simple_spinner_item,list);
To add a style to an adapter
Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);
The system provides the following styles
L Simple_spinner_dropdown_item (List-higher spacing more beautiful)
CLIP_IMAGE004[10]
L Simple_spinner_item (List-tight spacing is not good to see)
CLIP_IMAGE006[10]
L simple_list_item_checked (check box-Green ditch selected)
CLIP_IMAGE008[10]
L Simple_list_item_single_choice (radio button)
CLIP_IMAGE010[10]
Adding an adapter to a control spinner
1 Spinner sp= (Spinner) Findviewbyid (r.id.spinner1);
2 Sp.setadapter (adapter);
Interpretation
1. Setadapter (Spinneradapter adapter), Arrayadapter is an indirect implementation class of the Spinneradapter interface from the inheritance relationship of the class
CLIP_IMAGE012[10]
Implementing a Selection event (implementing an interface using an anonymous class)
1 Sp.setonitemselectedlistener (new Onitemselectedlistener () {
2//Parent: Spinner View for controls: TextView position for display text: The position of the drop-down option starts at 0
3 public void onitemselected (adapterview<?> parent, view view, int position, long ID) {
4 TextView Tvresult = (TextView) Findviewbyid (R.id.tvresult);
5//Get the adapter for the spinner control
6 Arrayadapter<string> adapter = (arrayadapter<string>) parent.getadapter ();
7 Tvresult.settext (Adapter.getitem (position));
8}
9//No processing when unchecked
Ten public void onnothingselected (adapterview<?> parent) {
11}
12});
Background overall code is as follows
1 public void OnCreate (Bundle savedinstancestate) {
2 super.oncreate (savedinstancestate);
3 Setcontentview (R.layout.main);
4 list<string> List = new arraylist<string> ();
5 List.add ("Apple");
6 List.add ("banana");
7 List.add ("Orange");
8 List.add ("banana");
9 Arrayadapter<string> adapter = new Arrayadapter<string> (this, Android. R.layout.simple_spinner_item, list);
Ten Adapter.setdropdownviewresource (Android. R.layout.simple_list_item_single_choice);
Spinner sp = (Spinner) Findviewbyid (r.id.spinner1);
Sp.setadapter (adapter);
Sp.setonitemselectedlistener (New Onitemselectedlistener () {
+//Parent: Spinner View for controls: TextView position for display text: The position of the drop-down option starts at 0
public void onitemselected (adapterview<?> parent, view view, int position, long ID) {
TextView Tvresult = (TextView) Findviewbyid (R.id.tvresult);
17//Get the adapter for the spinner control
arrayadapter<string> adapter = (arrayadapter<string>) parent.getadapter ();
Tvresult.settext (Adapter.getitem (position));
20}
21//No processing when unchecked
public void onnothingselected (adapterview<?> parent) {
23}
24});
25}
Use of the Android:spinner (drop-down box) control