Android Control---Spinner

Source: Internet
Author: User

The list items for the Spinner drop-down list box are configured in two ways:

1. Through the resource file configuration, the list item content is added by using the <string-array> element in the XML of the values, such as Strings.xml, and then through the android:entries= "@array/xxid" The way to link.

2. Read the resource file through the Android.widget.ArrayAdapter class or specify the specific settings data.

One: How the resource files are configured.

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stringname= "App_name">ADTest</string>    <stringname= "Hello_world">Hello world!</string>    <stringname= "Action_settings">Settings</string>        <String-arrayname= "Mycity">        <Item>Beijing</Item>        <Item>Chongqing</Item>        <Item>Shanghai</Item>    </String-array></Resources>

Configure the link through the android:entries= "@array/mycity" of the spinner species.

Two. Configure through the Arrayadapter class.

(1) Read the list items defined in the resource file

arrayadapter<charsequence> adapter = Arrayadapter.createfromresource (This, r.array.mycity, Android. R.layout.simple_spinner_item);  Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);   Spinner.setadapter (adapter);

(2) Read the development data

arraylist<charsequence> list =NewArraylist<charsequence>(); List.add (Beijing); List.add (Chongqing); List.add (Shanghai); List.add (Tianjin); Spinner Spinner=(Spinner) Findviewbyid (r.id.spinner1); Arrayadapter<CharSequence> adapter =NewArrayadapter<charsequence> ( ThisAndroid.        R.layout.simple_spinner_dropdown_item, list); Adapter.setdropdownviewresource (Android.        R.layout.simple_spinner_item); Spinner.setadapter (adapter);

Or

string[] items = {"Beijing", "Shanghai", "Chongqing", "Tianjin"};
Spinner Spinner = (Spinner) Findviewbyid (r.id.spinner1);

arrayadapter<charsequence> adapter = New Arrayadapter<charsequence> (this, Android. R.layout.simple_spinner_dropdown_item, items);

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

Spinner.setadapter (adapter);

Where the charsequence is the parent class interface of String,

Arrayadapter<charsequence> adapter = new arrayadapter<charsequence> ( Span style= "color: #0000ff;" >this, Android. R.layout.simple_spinner_dropdown_item, items) in Android. R.layout.simple_spinner_dropdown_item is the style when the list box is not expanded, while Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_item) is the style of the drop-down list after the settings are expanded.

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.