Android Spinner Menu Usage examples detailed _android

Source: Internet
Author: User
Tags event listener sqlite database stub

This example describes the use of the Android spinner menu. Share to everyone for your reference, specific as follows:

One, using arrays as data sources

1. In the layout layout file: Declare a TextView control and a spinner control with the following code

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

2. Add the following code to the activity file

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemSelectedListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.Spinner;
Import Android.widget.TextView;
  public class Spinneractivity extends activity {private static final string[] m={"Type a", "B", "O", "AB", "Other"};
  Private TextView view;
  Private Spinner Spinner;
  Private arrayadapter<string> adapter; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.oncreate
    (savedinstancestate);
    Setcontentview (R.layout.spinner);
    View = (TextView) Findviewbyid (R.id.spinnertext);
    Spinner = (spinner) Findviewbyid (R.ID.SPINNER01); Connect the optional content to the Arrayadapter adapter = new Arrayadapter<string> (this,android.
    R.LAYOUT.SIMPLE_SPINNER_ITEM,M); Set the style of the Drop-down list Adapter.setdropdownviewresource (Android.
    R.layout.simple_spinner_dropdown_item);Add the adapter to the spinner Spinner.setadapter (adapter);
    Add Event Spinner Event Listener Spinner.setonitemselectedlistener (New Spinnerselectedlistener ());
  Set default value spinner.setvisibility (view.visible); Use array form to manipulate class Spinnerselectedlistener implements onitemselectedlistener{public void onitemselected (Adaptervi
    Ew<?> arg0, View arg1, int arg2, long arg3) {View.settext ("Your Blood type is:" +m[arg2]);

 public void onnothingselected (Adapterview<?> arg0) {}}}

Ii. using XML as a data source

1. Create a new Arryas.xml file under the Values folder: Code as follows

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
  <string-array name= "Plantes" >
    <item>NOKIA</item>
    <item>MOTO</item>
    <item>HTC</item>
    <item >LG</item>
    <item> Other </item>
  </string-array>
</resources>

2. Add the following code to the activity file

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemSelectedListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.Spinner;
Import Android.widget.TextView;
  public class Spinneractivity extends activity {private TextView view2;
  Private Spinner Spinner2;
  Private Arrayadapter Adapter2; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub super.oncreate
    (savedinstancestate);
    Setcontentview (R.layout.spinner);
    Spinner2 = (Spinner) Findviewbyid (R.ID.SPINNER02);
    View2 = (TextView) Findviewbyid (r.id.spinnertext02); Connect the optional content to the Arrayadapter Adapter2 = Arrayadapter.createfromresource (this, R.array.plantes, Android.
    R.layout.simple_spinner_item); Set the style of the Drop-down list Adapter2.setdropdownviewresource (Android.
    R.layout.simple_spinner_dropdown_item); Add Adapter2 to spinner Spinner2.setadapter (adapter2);
    Add Event Spinner Event Listener Spinner2.setonitemselectedlistener (New Spinnerxmlselectedlistener ());
  Set default value spinner2.setvisibility (view.visible); Use XML to manipulate class Spinnerxmlselectedlistener implements onitemselectedlistener{public void onitemselected (Adapt
    Erview<?> arg0, View arg1, int arg2, long arg3) {View2.settext ("What kind of cell phone do you use:" +adapter2.getitem (arg2));

 public void onnothingselected (Adapterview<?> arg0) {}}}

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Resources Operating Skills Summary", "Android File Operating skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", The Android View view tips summary and the Android Control usage summary

I hope this article will help you with the Android program.

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.