Android Development-spinner Implementation of the drop-down box control

Source: Internet
Author: User

Android provides a very practical control spinner that implements the drop-down box feature.

The spinner control needs to add the spinner tag to the XML resource file, as follows:

<Spinner Android:id="@+id/spinner1"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_torightof="@+id/textview1"Android:layout_centerhorizontal="true"/> <TextView Android:id="@+id/textview1"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout_alignbottom="@+id/spinner1"Android:layout_alignparentleft="true"Android:layout_marginbottom="20DP"Android:layout_marginleft="16DP"android:textsize="20SP"Android:text="Selected City"/>

After you add the resource control, in the activity, add the code:

 PackageCom.example.android_spinner2;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.Spinner; Public classMainactivityextendsActivity {PrivateSpinner Spinner; PrivateArrayadapter Adapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);  This. Spinner = (spinner) This. Findviewbyid (R.id.spinner1); //two ways to register an adapter with a drop-down list//1:new Adapter Object (Content object, style ID, data collection)adapter =NewArrayadapter<string> ( This, Android.        R.layout.simple_spinner_dropdown_item, Getdatasource ());    Spinner.setadapter (adapter); }     PublicList<string>Getdatasource () {List<String> list =NewArraylist<string>() ; List.add (Beijing); List.add (Shanghai); List.add (Guangzhou); List.add (Changsha); returnlist; } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
Running in the simulation will result in a spinner display:

From the official API, the official offers another way to register an adapter for Spiner. The sample code is as follows:

    protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);  This. Spinner = (spinner) This. Findviewbyid (R.id.spinner1); //two ways to register an adapter with a drop-down list//1:new Adapter Object (Content object, style ID, data collection)//adapter = new Arrayadapter<string> (This,//Android.        R.layout.simple_spinner_dropdown_item, Getdatasource ()); //2:. Createfromresource (Content object, array ID in XML, style ID)adapter = Arrayadapter.createfromresource (mainactivity. This, R.array.planets_array,android.        R.layout.simple_spinner_dropdown_item);    Spinner.setadapter (adapter); }

Note that the second way to add an array to the string resource file is:

   <string-array name= "Planets_array" >        <item>Mercury</item>        <item>venus</item >        <item>Earth</item>    </string-array

You can also get the list of drop-down boxes you want by running the project.

  

Android Development-spinner Implementation of the drop-down box 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.