Android app development notes-drop-down list (Spinner)

Source: Internet
Author: User

Drag the icon on the panel to the interface, and add the following code in res/strings. xml:

<String name = "spinner_prompt"> Blood type </string>

<String-array name = "bloods">

<Item> Boold Types A </item>

<Item> Boold Types B </item>

<Item> Boold Types O </item>

<Item> Boold Types AB </item>

</String-array>

First, add the description text of the List header;

The second item is the content of each item in the list;

Finally, modify related items in Properties, such:

 

 

 

 

 

 

The activity_main.xml list is as follows:

<Spinner

Android: id = "@ + id/spin01"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: layout_alignRight = "@ + id/btnOk"

Android: layout_below = "@ + id/radioGroup01"

Android: layout_marginTop = "22dp"

Android: entries = "@ array/bloods"

Android: prompt = "@ string/spinner_prompt"/>

 

Then, we start encoding.

The related code is as follows:


[Java]
// Spinner
M_adapterView = ArrayAdapter. createFromResource (
This, R. array. bloods, android. R. layout. simple_spinner_dropdown_item );

M_adapterView.setDropDownViewResource (
Android. R. layout. simple_spinner_dropdown_item );

M_spinner = (Spinner) findViewById (R. id. spin01 );
M_spinner.setAdapter (m_adapterView );
M_spinner.setOnItemSelectedListener (this );

// Spinner
M_adapterView = ArrayAdapter. createFromResource (
This, R. array. bloods, android. R. layout. simple_spinner_dropdown_item );

M_adapterView.setDropDownViewResource (
Android. R. layout. simple_spinner_dropdown_item );

M_spinner = (Spinner) findViewById (R. id. spin01 );
M_spinner.setAdapter (m_adapterView );
M_spinner.setOnItemSelectedListener (this );
 

The statement is as follows:

ArrayAdapter <CharSequence> m_adapterView;

Spinnerm_spinner;

 

CharSequence must be used in the wildcard of ArrayAdapter.

 

You can use either of the following code to obtain the selected string:

1. Read direct resource files

This. getResources (). getStringArray (R. array. bloods) [arg2]

2. Read the selected item

M_spinner.getSelectedItem (). toString ()

 

We recommend 2nd methods.

 

 

The effect is as follows:

 

 

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.