Android Control Development spinner controls

Source: Internet
Author: User

Android Control Development spinner controlsOverview: In Android, the spinner control is primarily used to display a drop-down list, while the user can select the data in the list as the current selection
Java code:
There are two methods used in this code to provide data to spinner (Method 1 and Method 2). Run-time optionalmethod One:use the form of dynamic list to provide data to spinner Method Two:uses a fixed string array defined in the Strings.xml to provide data according to the project needs, select the relevant method can
Package com.example.spinnertest;


Import java.util.ArrayList;
Import java.util.List;


Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.Adapter;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemSelectedListener;
Import Android.widget.ArrayAdapter;
Import Android.widget.Spinner;


public class Mainactivity extends Activity {


Private Spinner myspinner = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Myspinner = (Spinner) Findviewbyid (R.id.myspinner);

Method 1
When the spinner entry is not fixed, the custom list is used to display the style of the pop-up entry that can be laid out by designing the itme.xml.
/*list<string> List = new arraylist<string> ();

List.add ("Test0");
List.add ("Test1");
List.add ("Test2");
List.add ("Test3");

arrayadapter<charsequence> adapter = new Arrayadapter (this, R.layout.item, R.id.myitem, list); */
Method 1 End

Method 2
Spinner displays an array of fixed entries in the Strings.xml file
The third parameter is the style of setting the spinner, using the system's layout file, or using your own layout file
arrayadapter<charsequence> adapter = Arrayadapter.createfromresource (This,
R.array.plant_list,
Android. R.layout.simple_spinner_item);
Set the style for each entry in the spinner, using the system layout file, or using your own layout file
Adapter.setdropdownviewresource (Android. R.layout.select_dialog_singlechoice);
Method 2 End


Myspinner.setadapter (adapter);

Set the title of the spinner
Myspinner.setprompt ("test");

Binding to item-bound listeners in list lists
Myspinner.setonitemselectedlistener (New Myspinnerselectitemlistener ());

}


The inner class is the listener for item in the popup list
Class Myspinnerselectitemlistener implements onitemselectedlistener{


@Override
public void onitemselected (adapterview<?> adapterview, view view, int pos,
Long id) {
TODO auto-generated Method Stub
Long Selectedid = Adapterview.getitemidatposition (POS);
String selected = Adapterview.getitematposition (pos). toString ();
System.out.println (Selectedid + "+": "+ selected);
}


@Override
public void onnothingselected (adapterview<?> arg0) {
TODO auto-generated Method Stub
System.out.println ("do nothing");
}

}


@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}


}



The Strings.xml code is as follows: <?xml version= "1.0" encoding= "Utf-8"?>
<resources>


    <string name= "App_name" >SPINNERTEST</STRING>
    <string name= "action_settings" >SETTINGS</STRING>
    <string name= "Hello_world" >hello world!</string>


    <string-array name = "Plant_list";
        <item>tan0</item>,
        <ITEM>TAN1</ITEM>
        <item>tan2</item>
        <ITEM>TAN3</ITEM>
        <item>tan4</item>
        <item>tan5</item>
            
        </STRING-ARRAY>
    
</resources>

The main layout main.xml code is as follows: <menu xmlns:android= "Http://schemas.android.com/apk/res/android" >


<item
Android:id= "@+id/action_settings"
android:orderincategory= "100"
Android:showasaction= "Never"
android:title= "@string/action_settings"/>

</menu>

The results are as follows:

Android Control Development spinner controls

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.