The simple use of spinner controls for Android application development _android

Source: Internet
Author: User

There are many kinds of Android controls, one of which is a spinner control, which is actually a drop-down display list. The spinner is located under the Android.widget package, displaying only the elements selected by the user each time, and when the user clicks again, the selection list is ejected for the user to select, and the elements from the selection list are also from the adapter. Spinner is a subclass of the view class.

First look at the spinner Effect chart:

Code:

Mainactivity

Package com.mecury.spinnertest;
Import java.util.ArrayList;
Import android.support.v7.app.ActionBarActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.ArrayAdapter;
Import Android.widget.Spinner;
  public class Mainactivity extends Actionbaractivity {private Spinner Spinnerbutton;
  Private Spinner Spinner;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    Spinnerbutton = (Spinner) Findviewbyid (R.id.spinnerbutton);
    Spinner = (spinner) Findviewbyid (R.ID.SPINNER2); /* Static display of the menu options, the display of the array elements in advance has been set * The second parameter: already prepared the array * Third data: Default Style * * arrayadapter<charsequence> ADA Pter = 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 Android provided layout file Adapter.setdropdownviewresource. R.layout.simple_spinner_dropdOwn_item);
    Spinnerbutton.setadapter (adapter);
    Spinnerbutton.setprompt ("test");
    Spinnerbutton.setonitemselectedlistener (New Spinnerlistener ());
    * * Dynamically add the option to display the menu, you can animate the element * * arraylist<string> list = new arraylist<string> ();
    List.add ("1 apple");
    List.add ("2 oranges"); * * The second parameter is the layout of the display * The third parameter is the position ID in the layout display * The fourth parameter is the data to be displayed/* Arrayadapter adapter2 = new Arrayadapter (th
    is, R.layout.item, r.id.textview,list);
    Spinner.setadapter (ADAPTER2);
  Spinner.setonitemselectedlistener (New Spinner2listener ()); Class Spinnerlistener implements android.widget.adapterview.onitemselectedlistener{@Override public void Onit Emselected (adapterview<?> Parent, view view, int position, long ID) {//To display the selected elements String select
    ed = parent.getitematposition (position). ToString (); @Override public void onnothingselected (adapterview<?> parent) {System.out.println ("Nothingselect")
    ;Class Spinner2listener implements android.widget.adapterview.onitemselectedlistener{@Override public void Onitemselected (adapterview<?> Parent, view view, int position, long id) {String selected = parent.ge
    Titematposition (position). ToString (); @Override public void onnothingselected (adapterview<?> parent) {System.out.println ("Nothingselect")
    ; }
  }
}

Code for Main_activity:

<linearlayout xmlns: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 ">
  <textview 
    android:layout_width=" Match_parent "
    Android : layout_height= "wrap_content"
    android:text= "This is static:"/>
  <spinner android:id= 
    "@+id/ Spinnerbutton "
    android:layout_width=" match_parent "
    android:layout_height=" 30DP "/>
  < TextView 
    android:layout_width= "match_parent"
    android:layout_height= "wrap_content"
    android:text= "This is dynamic:"/>
  <spinner 
    android:id= "@+id/spinner2"
    android:layout_width= "Match_parent
    " android:layout_height= "Wrap_content"/>
</LinearLayout>

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

<string-array name= "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"?> <linearlayout xmlns: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" >
  <textview 
    Android:id= "@+id/textview"
    android:layout_width= "match_parent"
    android:layout_height= "Wrap_content"
    android:textsize= "20DP"/>
</LinearLayout>

The above content is about the Android application development spinner control simple use, hoped is helpful to everybody.

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.