Simple use of android spinner, androidspinner

Source: Internet
Author: User

Simple use of android spinner, androidspinner

First look at the spinner's:

 

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 (Bu Ndle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); spinnerButton = (Spinner) findViewById (R. id. spinnerButton); spinner = (Spinner) findViewById (R. id. spinner2);/* the menu options displayed statically. The displayed array element has been set in advance * second parameter: Prepared array * Third data: default style */ArrayAdapter <CharSequence> adapter = ArrayAdapter. createFromResource (this, R. array. number_array, android. r. layout. simple _ Spinner_item); // sets the style of each entry in the spinner. The layout file adapter provided by android is also referenced. setDropDownViewResource (android. r. layout. simple_spinner_dropdown_item); spinnerButton. setAdapter (adapter); spinnerButton. setPrompt ("test"); spinnerButton. setOnItemSelectedListener (new spinnerListener ();/** dynamically add menu options to dynamically add elements */ArrayList <String> list = new ArrayList <String> (); list. add ("1. apple "); list. add ("2. orange ");/** the second parameter is the display Layout * The third parameter is the id of the position displayed in the layout * The fourth parameter is the data to be displayed */ArrayAdapter adapter2 = new ArrayAdapter (this, R. layout. item, R. id. textview, list); spinner. setAdapter (adapter2); spinner. setOnItemSelectedListener (new spinner2Listener ();} class spinnerListener implements android. widget. adapterView. onItemSelectedListener {@ Override public void onItemSelected (AdapterView <?> Parent, View view, int position, long id) {// display the selected element String selected = 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. getItemAtPosition (position). toString () ;}@ Override public void onNothingSelected (AdapterView <?> Parent) {System. out. println ("nothingSelect ");}}}

Main_activity code:

<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 method is static implementation, we need to set the content to be displayed by the spinner in advance. Add the content to be displayed 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 method is static implementation. 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>

 

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.