How to use Android drop-down menu spinner

Source: Internet
Author: User

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
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:id = "@+id/textview_city"
  9. android:layout_width = "Wrap_content"
  10. android:layout_height = "Wrap_content"
  11. Android:text = "City:"
  12. />
  13. <!--define a city information drop-down menu --
  14. <Spinner
  15. android:id = "@+id/spinner_city"
  16. android:layout_width = "Wrap_content"
  17. android:layout_height ="Wrap_content" >
  18. </Spinner >
  19. </linearlayout>

(3) Spinneractivity.java code.

[Java]View Plaincopy
  1. Package cn.com;
  2. Import java.util.ArrayList;
  3. Import java.util.List;
  4. Import android.app.Activity;
  5. Import Android.os.Bundle;
  6. Import android.view.MotionEvent;
  7. Import Android.view.View;
  8. Import Android.widget.AdapterView;
  9. Import Android.widget.ArrayAdapter;
  10. Import Android.widget.Spinner;
  11. Import Android.widget.TextView;
  12. Public class Spinneractivity extends Activity {
  13. /** Called when the activity is first created. * /
  14. private List<string> List = new arraylist<string> ();
  15. private TextView Mytextview;
  16. private Spinner Myspinner;
  17. private arrayadapter<string> adapter;
  18. @Override
  19. public void OnCreate (Bundle savedinstancestate) {
  20. super.oncreate (savedinstancestate);
  21. Setcontentview (R.layout.main);
  22. //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
  23. List.add ("Beijing");
  24. List.add ("Shanghai");
  25. List.add ("Shenzhen");
  26. List.add ("Fuzhou");
  27. List.add ("Xiamen");
  28. Mytextview = (TextView) Findviewbyid (r.id.textview_city);
  29. Myspinner = (Spinner) Findviewbyid (r.id.spinner_city);
  30. ///Step Two: Define an adapter for the drop-down list, using the list defined in the previous section.
  31. adapter = New Arrayadapter<string> (this,android.    R.layout.simple_spinner_item, list);
  32. //Step three: Set the menu style for the adapter drop-down list.
  33. Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);
  34. //Step Fourth: Add the adapter to the drop-down list
  35. Myspinner.setadapter (adapter);
  36. //Fifth step: Set the response of various events for the drop-down list, the Response menu is selected
  37. Myspinner.setonitemselectedlistener (new Spinner.onitemselectedlistener () {
  38. public void onitemselected (adapterview<?> arg0, View arg1, int arg2, long arg3) {
  39. //TODO auto-generated method stub
  40. / * Bring the value of the selected Myspinner into the Mytextview * /
  41. Mytextview.settext ("You have selected:" + Adapter.getitem (arg2));
  42. / * Display the Myspinner * /
  43. Arg0.setvisibility (view.visible);
  44. }
  45. public void onnothingselected (adapterview<?> arg0) {
  46. //TODO auto-generated method stub
  47. Mytextview.settext ("NONE");
  48. Arg0.setvisibility (view.visible);
  49. }
  50. });
  51. / * drop-down menu popup content options Touch-screen event handling * /
  52. Myspinner.setontouchlistener (new Spinner.ontouchlistener () {
  53. Public Boolean OnTouch (View V, motionevent event) {
  54. //TODO auto-generated method stub
  55. /** 
  56. *
  57. */
  58. return false;
  59. }
  60. });
  61. / * drop-down menu popup content options Focus Change Event handling * /
  62. Myspinner.setonfocuschangelistener (new Spinner.onfocuschangelistener () {
  63. public void Onfocuschange (View V, boolean hasfocus) {
  64. //TODO auto-generated method stub
  65. }
  66. });
  67. }
  68. }

The results of the operation are as follows:



How to use Android drop-down menu 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.