Android uses spinner to implement a drop-down list

Source: Internet
Author: User

Course Objectives
1. Understand the use and functionality of the spinner drop-down list
2. Learn to use the system default spinner
3. Learn to use custom style spinner

Perform steps
First step: Add a list of drop-down list items, and the items you add here are the menu items for the drop-down list
Private list<string> List = new arraylist<string> ();
List.add ("background");
List.add ("Shanghai");
List.add ("Guangzhou");
List.add ("Shenzhen");
Step two: Define an array adapter (Arrayadapter) for the drop-down list, where the list defined above is used.
adapter = new Arrayadapter<string> (this, Android. R.layout.simple_spinner_item, list);
------> Define an adapter, add a data source
Step Three: Set the menu style for the adapter drop-down list.
Adapter.setdropdownresource (Android. R.layout.simple_spinner_dropdown_item);
Fourth step: Add the adapter to the drop-down list-spinner load Adapter
Myspinner.setadapter (adapter);
Fifth step: Set the response of various time for the drop-down list, this event response menu is selected
Myspinner.setonitemselectedlistener (New Spinner.onitemselectedlistener ())
--Set listener for Spiner

The city name can be obtained using Adapter.getitem (position) (of course, the city name can also be obtained by list.get (position) in this program).

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"    >    <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:textsize= "25SP"Android:textcolor= "#FF00FF"        />    <SpinnerAndroid:id= "@+id/spinner1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /></LinearLayout>
Activity_main.xml
 PackageCom.example.spinner;Importjava.util.ArrayList;Importjava.util.List;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemSelectedListener;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.Spinner;ImportAndroid.widget.TextView; Public classMainactivityextendsActionbaractivityImplementsOnitemselectedlistener {PrivateTextView TextView; PrivateSpinner Spinner; PrivateArrayadapter<string>Arrayadapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); TextView=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Textview.settext ("The city you chose is Beijing"); Spinner=(Spinner) Findviewbyid (r.id.spinner1); List<String> list =NewArraylist<string>();  for(String s:NewString[] {"Beijing", "Shanghai", "Guangzhou", "Hangzhou"}) List.add (s); Arrayadapter=NewArrayadapter<string> ( This, Android.        R.layout.simple_spinner_item, list); Arrayadapter.setdropdownviewresource (Android.        R.layout.simple_dropdown_item_1line);        Spinner.setadapter (Arrayadapter); Spinner.setonitemselectedlistener ( This); } @Override Public voidOnitemselected (adapterview<?> Parent, view view,intposition,LongID) {String cityname=Arrayadapter.getitem (position); Textview.settext ("The city you selected is" +cityname); } @Override Public voidOnnothingselected (adapterview<?>parent) {            }}
Mainactivity.java

Effect:

Android uses spinner to implement a drop-down list

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.