Android: Use of the Spinner (drop-down box) control.

Source: Internet
Author: User

Android: Use of the Spinner (drop-down box) control.

 

1.

2.Create a page 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" 10 android: text = "selected fruit"/>

3.Create a data source from the drop-down list 

1 List <String> list = new ArrayList <String> (); 2 list. add ("apple"); 5 list. add ("banana"); 7 list. add ("orange"); 9 list. add ("banana ");

4.Create an adapter(The data source in the drop-down list is from the adapter.) 

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);

5.Add a style for the adapter 

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

 

The system provides the following styles:

LSimple_spinner_dropdown_item (List-relatively high spacing)

LSimple_spinner_item (List-compact spacing is hard to see)

 

 

LSimple_list_item_checked(Check box-selected green troughs)

 

 

LSimple_list_item_single_choice (Single button)

 

 6.Forward ControlSpinnerAdd an adapter 

1       Spinner sp=(Spinner) findViewById(R.id.spinner1);2       sp.setAdapter(adapter); 

 

Meaning 

1. setAdapter (SpinnerAdapter adapter). From the inheritance relationship of the class, ArrayAdapter is an indirect implementation class of the SpinnerAdapter interface.

 

7. Implement the selected event (using the Anonymous class to implement the interface)

1 sp. setOnItemSelectedListener (new OnItemSelectedListener () {2 // parent: For the control Spinner view: TextView position of the displayed text: the position of the drop-down options starts from 0 3 public void onItemSelected (AdapterView <?> Parent, View view, int position, long id) {4 TextView tvResult = (TextView) findViewById (R. id. tvResult); 5 // obtain the adapter 6 ArrayAdapter of the Spinner Control <String> adapter = (ArrayAdapter <String>) parent. getAdapter (); 7 tvResult. setText (adapter. getItem (position); 8} 9 // process 10 public void onNothingSelected (AdapterView <?> Parent) {11} 12 });

8.The overall background 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); 10 adapter. setDropDownViewResource (android. r. layout. Simple_list_item_single_choice); 11 Spinner sp = (Spinner) findViewById (R. id. spinner1); 12 sp. setAdapter (adapter); 13 sp. setOnItemSelectedListener (new OnItemSelectedListener () {14 // parent: For the control Spinner view: TextView position of the displayed text: the position of the drop-down options starts from 0 15 public void onItemSelected (AdapterView <?> Parent, View view, int position, long id) {16 TextView tvResult = (TextView) findViewById (R. id. tvResult); 17 // obtain the adapter 18 ArrayAdapter for the Spinner Control <String> adapter = (ArrayAdapter <String>) parent. getAdapter (); 19 tvResult. setText (adapter. getItem (position); 20} 21 // process 22 public void onNothingSelected (AdapterView <?> Parent) {23} 24}); 25}

 

Related Article

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.