android--list selection box (Spinner)

Source: Internet
Author: User

Typically, if the list item that you want to display in the list selection box is known, you can save it in an array resource file, and then specify the list item for the list selection box by using an array resource. This allows you to implement a drop-down selection box without writing Java code.

1. Add a <spinner> tag to the layout file and specify the Android:entries property for it, with the following code:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/linearlayout1 "    android:layout_width=" Match_parent    " android:layout_height= "Match_parent"    android:orientation= "vertical"    tools:context= ". Mainactivity "><spinner android:entries=" @array/ctype "     android:layout_width=" Wrap_content "     Android : layout_height= "wrap_content"     android:id= "@+id/spinner"/></linearlayout>

Where the Android:entries property is used to specify the list item, if the property is not specified in the layout file, it can be specified in the Java code by specifying an adapter for it;

2. Write the array resource file for the specified list item and save it in the Res\values directory, where it is named Arrays.xml, adding an array of strings to the file, named CType, with the following code

<?xml version= "1.0" encoding= "UTF-8"?><resources>    <string-array name= "CType" >        <item >ID</item>        <item>student card</item>        <item>army card</item>        < Item>work card</item>        <item>Other</item>    </string-array></resources>

After adding a list selection box on the screen, you can use the GetSelectedItem () method of the list selection box to get the selected value of the list selection box, using the following code:

Spinner spinner=(Spinner) Findviewbyid (R.id.spinner); Spinner.getselecteditem ();

If you want to perform the appropriate processing after the user chooses a different list item, you can add the Onitemselectedlistener event listener to the list selection box. For example, to add a select list event listener for spinner, and to get the value of the selection in the onitemselected () method output to the log, you can use the following code:

Package Com.basillee.blogdemo;import Android.os.bundle;import Android.app.activity;import android.util.Log;import Android.view.view;import Android.widget.adapterview;import Android.widget.AdapterView.OnItemSelectedListener; Import Android.widget.spinner;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Spinner spinner= (Spinner) Findviewbyid (R.id.spinner); Spinner.setonitemselectedlistener (New Onitemselectedlistener () {@Overridepublic void onitemselected (adapterview<?> parent, View arg1,int POS, long ID) {//TODO auto-generated m Ethod stubstring result=parent.getitematposition (POS). toString ();//Gets the value of the selection log.i ("spinner", result);} @Overridepublic void onnothingselected (adapterview<?> arg0) {//TODO auto-generated Method stub});}}

The following describes how to specify a list item by specifying the way the adapter specifies the list.

(1) Create an adapter object, typically using the Arrayadapter class. In Android, there are two ways to create an adapter: One is created from an array resource file, and the other is created from a string array inside java.

    • To create an adapter from an array resource file, you need to use the Createfromresource () method of the Arrayadapter class with the following code:
Arrayadapter<charsequence> Adapter=arrayadapter.createfromresource (this,r.array.ctype,android. R.layout.simple_dropdown_item_1line);
    • Create the following from Java code
String[]ctype=new string[]{"ID", "Student card", "Army Card"}; Arrayadapter<string> adapter=new arrayadapter<string> (this, Android. R.layout.simple_dropdown_item_1line,ctype);

(2) Set the option style for the adapter drop-down list:

Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);

(3) associate an adapter with a select list box:

Spinner.setadapter (adapter);

android--list selection box (Spinner)

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.