The spinner control is also a list-type control, and its inheritance is as follows:
Java.lang.Object
? Android.view.View
? Android.view.ViewGroup
? Android.widget.adapterview<textends android.widget.adapter>
? Android.widget.AbsSpinner
? Android.widget.Spinner
Android.widget.Spinner inherits the Android.view.ViewGroup class.
In the Android UI development, Spinner (drop-down list) is always available, a simple custom Spinner production we just need to remember this important five steps, a Spinner can be applied and born.
(1) Create a new Android project with the name spinner. Build an activity at the same time, named Spinneractivity.
(2) Modify Res/layout/main.xml
[HTML]View Plaincopy
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id = "@+id/textview_city"
- android:layout_width = "Wrap_content"
- android:layout_height = "Wrap_content"
- Android:text = "City:"
- />
- <!--define a city information drop-down menu --
- <Spinner
- android:id = "@+id/spinner_city"
- android:layout_width = "Wrap_content"
- android:layout_height ="Wrap_content" >
- </Spinner >
- </linearlayout>
(3) Spinneractivity.java code.
[Java]View Plaincopy
- Package cn.com;
- Import java.util.ArrayList;
- Import java.util.List;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import android.view.MotionEvent;
- Import Android.view.View;
- Import Android.widget.AdapterView;
- Import Android.widget.ArrayAdapter;
- Import Android.widget.Spinner;
- Import Android.widget.TextView;
- Public class Spinneractivity extends Activity {
- /** Called when the activity is first created. * /
- private List<string> List = new arraylist<string> ();
- private TextView Mytextview;
- private Spinner Myspinner;
- private arrayadapter<string> adapter;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- //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
- List.add ("Beijing");
- List.add ("Shanghai");
- List.add ("Shenzhen");
- List.add ("Fuzhou");
- List.add ("Xiamen");
- Mytextview = (TextView) Findviewbyid (r.id.textview_city);
- Myspinner = (Spinner) Findviewbyid (r.id.spinner_city);
- ///Step Two: Define an adapter for the drop-down list, using the list defined in the previous section.
- adapter = New Arrayadapter<string> (this,android. R.layout.simple_spinner_item, list);
- //Step three: Set the menu style for the adapter drop-down list.
- Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);
- //Step Fourth: Add the adapter to the drop-down list
- Myspinner.setadapter (adapter);
- //Fifth step: Set the response of various events for the drop-down list, the Response menu is selected
- Myspinner.setonitemselectedlistener (new Spinner.onitemselectedlistener () {
- public void onitemselected (adapterview<?> arg0, View arg1, int arg2, long arg3) {
- //TODO auto-generated method stub
- / * Bring the value of the selected Myspinner into the Mytextview * /
- Mytextview.settext ("You have selected:" + Adapter.getitem (arg2));
- / * Display the Myspinner * /
- Arg0.setvisibility (view.visible);
- }
- public void onnothingselected (adapterview<?> arg0) {
- //TODO auto-generated method stub
- Mytextview.settext ("NONE");
- Arg0.setvisibility (view.visible);
- }
- });
- / * drop-down menu popup content options Touch-screen event handling * /
- Myspinner.setontouchlistener (new Spinner.ontouchlistener () {
- Public Boolean OnTouch (View V, motionevent event) {
- //TODO auto-generated method stub
- /**
- *
- */
- return false;
- }
- });
- / * drop-down menu popup content options Focus Change Event handling * /
- Myspinner.setonfocuschangelistener (new Spinner.onfocuschangelistener () {
- public void Onfocuschange (View V, boolean hasfocus) {
- //TODO auto-generated method stub
- }
- });
- }
- }
The results of the operation are as follows:
How to use Android drop-down menu spinner