Simple use of Android spinner

Source: Internet
Author: User

First look at the spinner:

Code:

Mainactivity

 Packagecom.mecury.spinnertest;Importjava.util.ArrayList;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.Spinner; Public classMainactivityextendsactionbaractivity {PrivateSpinner Spinnerbutton; PrivateSpinner Spinner; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Spinnerbutton=(Spinner) Findviewbyid (R.id.spinnerbutton); Spinner=(Spinner) Findviewbyid (R.ID.SPINNER2); /*static display down out of the menu options, displaying the array elements in advance has been set up * The second parameter: already well-written array * Third data: Default Style*/Arrayadapter<CharSequence> adapter =Arrayadapter.createfromresource ( This, R.array.number_array, Android.        R.layout.simple_spinner_item); //set the style for each entry in the spinner, and also refer to the layout file provided by AndroidAdapter.setdropdownviewresource (Android.        R.layout.simple_spinner_dropdown_item);        Spinnerbutton.setadapter (adapter); Spinnerbutton.setprompt (Test); Spinnerbutton.setonitemselectedlistener (NewSpinnerlistener ()); /** Dynamically add the option to display down menu, you can animate the element*/ArrayList<String> list =NewArraylist<string>(); List.add ("1. Apple"); List.add ("2. Orange"); /** The second parameter is the layout shown * The third parameter is the location shown in the layout ID * The fourth parameter is the data that will be displayed*/Arrayadapter Adapter2=NewArrayadapter ( This, R.layout.item, r.id.textview,list);        Spinner.setadapter (ADAPTER2); Spinner.setonitemselectedlistener (NewSpinner2listener ()); }    classSpinnerlistenerImplementsandroid.widget.adapterview.onitemselectedlistener{@Override Public voidOnitemselected (adapterview<?>Parent, view view,intPositionLongID) {//Display the selected elementsString selected =parent.getitematposition (position). ToString (); } @Override Public voidOnnothingselected (adapterview<?>parent) {System.out.println ("Nothingselect"); }    }    classSpinner2listenerImplementsandroid.widget.adapterview.onitemselectedlistener{@Override Public voidOnitemselected (adapterview<?>Parent, view view,intPositionLongID) {String selected=parent.getitematposition (position). ToString (); } @Override Public voidOnnothingselected (adapterview<?>parent) {System.out.println ("Nothingselect"); }    }}

Main_activity's Code:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"android:padding= "10DP"Tools:context= "Com.mecury.spinnertest.MainActivity" >    <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "This is static:"/>    <SpinnerAndroid:id= "@+id/spinnerbutton"Android:layout_width= "Match_parent"Android:layout_height= "30DP"/>    <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "This is dynamic:"/>    <SpinnerAndroid:id= "@+id/spinner2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"/></LinearLayout>

Because the first is a static implementation, we need to set the spinner to be displayed beforehand. Add what you want to display in the String.xml file:

    <String-arrayname= "Number_array">        <Item>1000</Item>        <Item>2000</Item>        <Item>3000</Item>        <Item>4000</Item>        <Item>5000</Item>        <Item>6000</Item>        <Item>7000</Item>        <Item>8000</Item>        <Item>9000</Item>    </String-array>

The second is the static implementation method, we use our own display layout item.xml.

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "#00ffff"android:padding= "10DP"android:orientation= "vertical" >        <TextViewAndroid:id= "@+id/textview"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:textsize= "20DP"/></LinearLayout>

Simple use of Android 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.