Drop-down list in Android

Source: Internet
Author: User

Tag: Use code to get the tag wrap com logs opd util

In web development, HTML provides a drop-down list implementation that uses the <select> element to implement a drop-down list in which each drop-down list item uses the <option> representation. This is an essential interactive component in web development, and the corresponding implementation in Android is spinner.

Method One:

Static display of Spinner options in resource mode:

<spinner     android:layout_width= "match_parent"    android:layout_height= "wrap_content"    android:id= "@ +id/spinner2 "    android:entries=" @array/spingarr "    />

Data files:

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

Usage 2:

Dynamically display the Spinner option in code mode to add data to Spinner with the adapter

Package Com.example.testspanner;import Java.util.arraylist;import Java.util.list;import android.app.Activity; Import Android.os.bundle;import Android.widget.arrayadapter;import Android.widget.spinner;public class MainActivity Extends Activity {private Spinner spinner;private list<string> data_list;private arrayadapter<string> arr_    Adapter        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.spinner);            Spinner = (spinner) Findviewbyid (R.id.spinner);        Data Data_list = new arraylist<string> ();        Data_list.add ("Beijing");        Data_list.add ("Shanghai");        Data_list.add ("Guangzhou");                Data_list.add ("Shenzhen"); Adapter arr_adapter= New Arrayadapter<string> (this, Android.        R.layout.simple_spinner_item, data_list); Set style Arr_adapter.setdropdownviewresource (Android.        R.layout.simple_spinner_dropdown_item); Load Adapter Spinner. Setadapter (Arr_adapter); 
Get value

Spinner.setonitemselectedlistener (New Onitemselectedlistener () {

@Override
public void onitemselected (adapterview<?> arg0, View arg1,
int arg2, long arg3) {
LOG.I ("Sssssssss", Data_list.get (arg2));

}

@Override
public void onnothingselected (adapterview<?> arg0) {
TODO auto-generated Method Stub

}
});

            }}

Layout file added:

<spinner     android:layout_width= "match_parent"    android:layout_height= "wrap_content"    android:id= "@ +id/spinner "    />
Category: Android

Drop-down list in Android

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.