Android Basic---->spinner use

Source: Internet
Author: User

  Spinner provides a quick way to select values from the collection. By default, a spinner displays the values that are currently selected. Touch Spinner displays a drop-down menu from which the user can select a value. Today we begin to spinner study.

The project structure is as follows:

Define a spinner in the XML file, as follows:

< Spinner     Android:id = "@+id/spinner"     android:layout_width= "Wrap_content"    android:layout_height= "Wrap_ Content "/>

To add a selection list to spinner, you need to specify a spinneradapter in your activity or fragment code.

Add a selection list to the spinner

The list content in spinner can come from any source, but requires a spinneradapter, such as if the selection is an array, then adapter is Arrayadapter, or the selection is from a database query, Then adapter is CursorAdapter.

For example, if the selection is predetermined, you can specify an array content in the string's resource file.

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <String-arrayname= "Spinner_array">        <Item>Linux</Item>        <Item>Huhx</Item>        <Item>Liuli</Item>        <Item>Chenhui</Item>        <Item>Android</Item>    </String-array></Resources>

Provide an example of a arrayadapter in activity or fragment, as follows:

Spinner = (spinner) Findviewbyid (R.id.spinner); Arrayadapter<CharSequence> adapter = Arrayadapter.createfromresource (this, R.array.spinner _array, Android. R.layout.simple_spinner_item); Adapter.setdropdownviewresource (R.layout.support_simple_spinner_dropdown_item); Spinner.setadapter (adapter);

The Createfromresource method allows you to create a arrayadapter from a character array. The second parameter is an array defined in the resource file, and the third parameter is the way to define the spinner selection. simple_spinner_item. XML is as follows:

<?XML version= "1.0" encoding= "Utf-8"?><TextViewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@android: Id/text1"style= "? Android:attr/spinneritemstyle"Android:singleline= "true"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:ellipsize= "Marquee"android:textalignment= "Inherit"/>

then you need to call the Setdropdownviewresource method to specify the layout in the spinner list: Support_simple_spinner_dropdown_item.xml as follows:

<?XML version= "1.0" encoding= "Utf-8"?><TextViewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@android: Id/text1"style= "? Attr/spinnerdropdownitemstyle"Android:singleline= "true"Android:layout_width= "Match_parent"Android:layout_height= "? Attr/dropdownlistpreferreditemheight"android:ellipsize= "Marquee"/>

responding to user-selected spinner events

When the user selects a row in the drop-down menu, the spinner object receives a on-item-selected event:

Spinner.setonitemselectedlistener (new  Adapterview.onitemselectedlistener () {    @Override    public voidintlong  id) {        "Viewa:" + View + ", Parent:" + parent + " , Position: "+ position +", ID: "+ ID";    }    @Override    publicvoid onnothingselected (adapterview<?> parent) {        "on Nothing Selected");    });

The results are as follows:

Use of Android Foundation---->spinner

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.