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

Source: Internet
Author: User

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

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

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"/>

 

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 ");

 

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

Add a style for the adapter

 

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

 

The system provides the following styles:

 

L simple_spinner_dropdown_item (list-relatively nice-looking with high spacing)

 

Clip_image004 [10]

 

L simple_spinner_item (list-compact spacing is hard to see)

 

Clip_image006 [10]

 

L simple_list_item_checked (check box-selected green ditch)

 

Clip_image008 [10]

 

L simple_list_item_single_choice (single choice button)

 

Clip_image010 [10]

 

Add an adapter to the control Spinner

 

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.

 

Clip_image012 [10]

 

Implement the selected event (using an 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 option 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 of the Spinner Control

6 ArrayAdapter <String> adapter = (ArrayAdapter <String>) parent. getAdapter ();

7 tvResult. setText (adapter. getItem (position ));

8}

9 // processing when no selection is made

10 public void onNothingSelected (AdapterView <?> Parent ){

11}

12 });

 

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 option starts from 0

15 public void onItemSelected (AdapterView <?> Parent, View view, int position, long id ){

16 TextView tvResult = (TextView) findViewById (R. id. tvResult );

17 // get the adapter of the Spinner Control

18 ArrayAdapter <String> adapter = (ArrayAdapter <String>) parent. getAdapter ();

19 tvResult. setText (adapter. getItem (position ));

20}

21 // processing when no selection is made

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.