Implementation of the Android spinner Control

Source: Internet
Author: User

The screen of the mobile phone is small, so it is a good way to use the drop-down list for selective input. Like ListView, Spinner is an indirect subclass of AdapterView and a window for displaying data. Common Methods of the Spinner class are as follows. getItemAtPosition (Spinner. getSelectedItemPosition (); call the setOnItemSelectedListener () method to obtain the value of the drop-down list box, process the selected event in the drop-down list box, and set AdapterView. the OnItemSelectedListener instance is passed into strings as a parameter. xml [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> ASpinnerDemo </string> <string name = "hello_world"> Hello world! </String> <string name = "menu_settings"> Settings </string> <string name = "selectStr"> what is your choice? </String> </resources> main. xml [html] www.2cto.com <RelativeLayout xmlns: 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" tools: context = ". mainActivity "> <TextView android: id =" @ + id/TextView_Show "android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: t Ext = "@ string/selectStr" android: textSize = "16sp"> </TextView> <Spinner android: id = "@ + id/spinner_City" android: layout_width = "fill_parent" android: layout_marginTop = "50dp" android: layout_height = "wrap_content"> </Spinner> <! -- Define a drop-down menu --> </RelativeLayout> java code implementation [java] package com. example. aspinnerdemo; 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. view. menu; import android. widget. adapterView; import android. widget. arrayAdapter; import android. widget. spinner; import android. widget. tex TView; public class MainActivity 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 protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // Step 1: Add a drop-down list List of list items. The items added here are the list of menu items in the drop-down list. add ("Test 1"); list. add ("Test 2"); list. add ("Test 3"); list. add ("Test 4"); list. add ("Test 5"); list. add ("Test 6"); list. add ("Test 7"); list. add ("Test 8"); list. add ("test 9"); myTextView = (TextView) findViewById (R. id. textView_Show); mySpinner = (Spinner) findViewById (R. id. spinner_City); // Step 2: Define an adapter for the drop-down list. The previously defined list is used here. Adapter = new ArrayAdapter <String> (this, android. R. layout. simple_spinner_item, list); // Step 3: Set the menu style in the drop-down list for the adapter. Adapter. setDropDownViewResource (android. r. layout. simple_spinner_dropdown_item); // Step 4: add the adapter to the drop-down list. setAdapter (adapter); // Step 5: Set the event response for the drop-down list. 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 to myTextView */myTextView. setText ("You selected:" + adapter. getItem (arg2);/* display mySpinner */arg0.setVisibility (View. VISIBLE);} public void onNothingSelected (AdapterView <?> Arg0) {// TODO Auto-generated method stub myTextView. setText ("NONE"); arg0.setVisibility (View. VISIBLE) ;}});/* content options displayed in the drop-down menu touch-screen event processing */mySpinner. setOnTouchListener (new Spinner. onTouchListener () {public boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stub/* hides mySpinner. You can also hide it without hiding it, view your hobbies */v. setVisibility (View. INVISIBLE); return false ;}});/* The content option in the drop-down menu changes the focus of event processing */mySpinner. setOnFocusChangeListener (new Spinner. onFocusChangeListener () {public void onFocusChange (View v, boolean hasFocus) {// TODO Auto-generated method stub v. setVisibility (View. VISIBLE) ;}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. activity_main, menu); return true ;}}

Related Article

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.