Android Spinner (drop-down box) control in the use of the detailed _android

Source: Internet
Author: User

Android gives us a spinner control, which is basically a list, so let's talk about the control, which has been seen before, but today it's a new one.

Spinner is located under the Android.widget package, displaying only the elements selected by the user each time, and when the user clicks again, the selection list is ejected for the user to select, and the elements from the selection list are also from the adapter. Spinner is a subclass of the view class.

1. Effect drawing

2. Create a paging file (Main.xml)

<spinner
android:id= "@+id/spinner1"
android:layout_width= "Match_parent"
Wrap_content "/>
<textview
android:id= @+id/tvresult" android:layout_width= "Match_parent"
android:layout_height= "wrap_content"
android:text= "Selected fruit"/>

3. Create a drop-down box data source

list<string> list = new arraylist<string> ();
List.add ("Apple"); 
List.add ("banana"); 
List.add ("Orange"); 
List.add ("banana");

4. 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);

5. Adding styles to Adapters

Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);

The system provides the following style

Simple_spinner_dropdown_item (List-high spacing better looking)

Simple_spinner_item (List-tight spacing is not easy to see)

simple_list_item_checked (check box-selected with green ditch)

Simple_list_item_single_choice (radio button)

6. Add adapters to control spinner

Spinner sp= (Spinner) Findviewbyid (r.id.spinner1);

Interpretation

1. Setadapter (Spinneradapter adapter), seen from the inheritance relationship of the class, Arrayadapter is the indirect implementation class of the Spinneradapter interface


7. Implementing the selection event (using an anonymous class to implement the interface)

Sp.setonitemselectedlistener (New Onitemselectedlistener () {
//Parent: Spinner View for control: Display text TextView Position: The position of the dropdown option starts with 0 public 
void onitemselected (adapterview<?> parent, view view, int position, long ID) {
TextView Tvresult = (TextView) Findviewbyid (r.id.tvresult);
Gets the adapter for the spinner control
arrayadapter<string> adapter = (arrayadapter<string>) parent.getadapter ();
Tvresult.settext (Adapter.getitem (position));
}
Processing public
void onnothingselected (adapterview<?> parent) {
}) is not selected
;

8. The whole background code is as follows

public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main);
list<string> list = new arraylist<string> ();
List.add ("Apple");
List.add ("banana");
List.add ("Orange");
List.add ("banana"); arrayadapter<string> adapter = new Arrayadapter<string> (this, Android.
R.layout.simple_spinner_item, list); 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 control: Display text TextView  Position: The location of the dropdown option starts at 0 public void onitemselected (adapterview<?> parent, view view, int position, long id) {TextView
Tvresult = (TextView) Findviewbyid (R.id.tvresult);
Gets the adapter for the spinner control arrayadapter<string> adapter = (arrayadapter<string>) parent.getadapter ();
Tvresult.settext (Adapter.getitem (position)); /////No processing public void onnothingselected When selected (ADAPTERVIEW&LT;?> Parent) {}}); }

The above is a small set to introduce the Android spinner (dropdown box) control of the use of detailed, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.