Android Combat Simple Tutorial-first shot (spinner control details)

Source: Internet
Author: User

This tutorial is simple and practical, people like the words can pay attention to me, thank you!

A drop-down list box is a common graphical component that can effectively save screen space compared to other selection components and can be implemented using the Android.widget.Spinner class in Android.

The list items in the drop-down list box are configured in the following two ways.

Method one, through the resource file configuration, such as the definition of a values\city_data.xml file, when defining the content of the data need to use the <string-array> element designation, the definition is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><resources><string-array name= "City_labels" >    <item > Beijing </item>    <item> Shanghai </item>    <item> Guangzhou </item>    <item> Shenzhen </ Item></string-array></resources>


Mode two, read the resource file through the Android.widget.ArrayAdapter class or specify the data of the specific setting.

Way One

1. Define the Main.xml file

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <textview        android:id= "@+id/text"        android:layout_width= "Fill_parent"        android:layout_height= "wrap_content"        android:text= "Please choose your preferred city:"/>    <spinner        android:id= "@ +id/spinner "        android:layout_width=" fill_parent "        android:layout_height=" Wrap_content "        android: entries= "@array/city_labels" ><!--Load Data-    </Spinner></LinearLayout>


Actually this is what you can run the simulator for:

This is to see that the data has been added to the spinner inside, we found that the control is only without fragrance, no role, to achieve the monitoring how to do it?

Let's change the Main.xml file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <textview        android:layout_width= "fill_parent"        android:layout_height= " Wrap_content "        android:text=" Please select your preferred city: "/>    <spinner        android:id=" @+id/spinner "        android: Layout_width= "Fill_parent"        android:layout_height= "wrap_content"        android:entries= "@array/city_labels" > <!--Loading Data-    </Spinner>    <textview        android:id= "@+id/text"        android:layout_ Width= "Fill_parent"        android:layout_height= "Wrap_content"/></linearlayout>

Then edit the mainactivity file:

Package Org.yayun.demo;import Org.yayun.demo.r;import Android.app.activity;import android.os.bundle;import Android.view.view;import Android.widget.adapterview;import Android.widget.textview;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.spinner;public class MainActivity extends Activity {private Spinner spinner;private TextView textview;public void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Life cycle Method Super.setcontentview (R.layout.main); Set the layout manager to use Spinner = (Spinner) Findviewbyid (r.id.spinner); TextView = (TextView) Findviewbyid (R.id.text); Spinner.setonitemselectedlistener (New Onitemselectedlistener () {public void onitemselected (adapterview<?> Parent, View view,int position, long ID) {string[] cities = getresources (). Getstringarray (r.array.city_labels);// Get list Data Textview.settext ("Your favorite city is:" + cities[position]);//display}public void onnothingselected (adapterview<?> Parent) {//TODO auto-generated Method stub}});}}

Run as follows:



Way Two

To modify the Main.xml code:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "/http Schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Andro id:orientation= "vertical" > <textview android:layout_width= "fill_parent" android:layout_height= "Wra P_content "android:text=" Please select your preferred city: "/> <spinner android:id=" @+id/spinner "Android:layout_wi Dth= "Fill_parent" android:layout_height= "wrap_content" android:entries= "@array/city_labels" > <!--loading        ---</Spinner> <textview android:id= "@+id/text" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"/> <spinner android:id= "@+id/spinnercountry" Android:layout_ Width= "Fill_parent" android:layout_height= "Wrap_content" > <!--dynamically load data--</spinner></linearl Ayout> 

Modify the Mainactivity program:

Package Org.yayun.demo;import Java.util.arraylist;import Java.util.list;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;import Android.widget.toast;public class MainActivity extends Activity {private Spinner Spinner, spinnercountry;private TextView textview;private list<charsequence> data = null ;p rivate arrayadapter<charsequence> adapter;public void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Life cycle Method Super.setcontentview (R.layout.main); Set the layout manager to use Spinner = (Spinner) Findviewbyid (r.id.spinner); TextView = (TextView) Findviewbyid (R.id.text); Spinnercountry = (Spinner) Findviewbyid (r.id.spinnercountry); Spinnercountry.setprompt ("Select Nationality:");//Display data = new in the list Arraylist<charsequence> ();d ata.add ("China");d Ata.add ("The United States");d Ata.add ("Japan");Dapter = new Arrayadapter<charsequence> (this,android. R.layout.simple_spinner_dropdown_item, This.data);//define drop-down list spinnercountry.setadapter (adapter); Spinner.setonitemselectedlistener (New Onitemselectedlistener () {public void onitemselected (adapterview<?> Parent, View view,int position, long ID) {string[] cities = getresources (). Getstringarray (r.array.city_labels);// Get list Data Textview.settext ("Your favorite city is:" + cities[position]);//display}public void onnothingselected (adapterview<?> Parent) {//TODO auto-generated Method stub}}); Spinnercountry.setonitemselectedlistener (new Onitemselectedlistener () {public void onitemselected (adapterview<?> parent, View view,int position, long ID) {string[] countries = Data.toarr Ay (new String[data.size ()));//Get list data Toast.maketext (Mainactivity.this, "Your nationality is:" +countries[position], Toast.length_ Short). Show ();} public void onnothingselected (adapterview<?> parent) {//TODO auto-generated Method stub}});}}


Run the following example:

Summarize:

1. Use <String-array> element designation to define data content;

2.android:Entries= "@array/city_labels" loaded into the text resource;

3.string[] Cities = getresources (). Getstringarray (r.array.city_labels);//methods of obtaining resource data

4.String and Charsequence relationships
String is inherited from Charsequence, which means that string is also the charsequence type.
Charsequence is an interface that includes only the length (), charAt (int index), subsequence (int start, int end) API interfaces. In addition to string implementations of Charsequence, StringBuffer and StringBuilder also implement the Charsequence interface.
It is necessary to note that Charsequence is a sequence of characters, String, StringBuilder and StringBuffer are essentially implemented by character arrays!

5. Settings for the message: Spinnercountry. setprompt ("Select Nationality:");//Display in List

6. Adapter can also be used. Setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);//To set the display style

Easy Android Walkthrough-first shot (spinner control)

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.