Five parts of Android spinner

Source: Internet
Author: User

After reading this piece of article, we can customize the spinner, then we will come to see what we are waiting for. 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, the name is spinnertest . Modify the main.xml under layout to add a Textview and a Spinner , the contents of the file are as follows:

<LinearLayoutAndroid:id= "@+id/widget28"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "vertical"xmlns:android= "Http://schemas.android.com/apk/res/android" ><TextViewAndroid:id= "@+id/textview_show"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "You chose to be"android:textsize= "25SP"></textview><SpinnerAndroid:id= "@+id/spinner_city"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"></spinner>< !-- define a drop -down menu--></linearlayout>

(2) Modify your spinnertest class, here we are going to remember five steps to customize a Spinner , complete code and five-step comments are as follows:

 PackageEoe.spinner;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnTouchListener;Importandroid.view.animation.Animation;Importandroid.view.animation.AnimationUtils;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.Spinner;ImportAndroid.widget.TextView; Public classSpinnerTest1extendsActivity {/**Called when the activity is first created.*/Privatelist< string> list =Newarraylist< string>();PrivateTextView Mytextview;PrivateSpinner Myspinner;Privatearrayadapter< string>adapter;PrivateAnimation myanimation; @Override Public voidonCreate (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 listList.add ("Beijing")); List.add (Shanghai); List.add (Shenzhen); List.add (Nanjing); List.add (Chongqing); Mytextview=(TextView) Findviewbyid (r.id.textview_show); Myspinner=(Spinner) Findviewbyid (r.id.spinner_city);//Step Two: Define an adapter for the drop-down list, which is used in the list defined in the previous section. adapter =Newarrayadapter< 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);//Fourth Step: Add the adapter to the drop-down listMyspinner.setadapter (adapter);//Fifth Step: Set the response of various events for the drop-down list, the Response menu is selectedMyspinner.setonitemselectedlistener (NewSpinner.onitemselectedlistener () { Public voidonitemselected (adapterview<?> arg0, View arg1,intArg2,LongArg3) {//TODO auto-generated Method Stub/*bring the value of the selected Myspinner into the Mytextview*/Mytextview.settext ("You have chosen:" +Adapter.getitem (arg2));/*Display the Myspinner*/arg0.setvisibility (view.visible);} Public voidonnothingselected (adapterview<?>arg0) {//TODO auto-generated Method StubMytextview.settext ("NONE"); arg0.setvisibility (view.visible);}});/*Drop -down menu popup content options Touch-screen event handling*/Myspinner.setontouchlistener (NewSpinner.ontouchlistener () { Public BooleanOnTouch (View V, motionevent event) {//TODO auto-generated Method Stub/*will myspinner hide, do not hide also can, see oneself hobby*/v.setvisibility (view.invisible);return false;}});/*Drop -down menu Popup content options Focus Change event handling*/Myspinner.setonfocuschangelistener (NewSpinner.onfocuschangelistener () { Public voidOnfocuschange (View V,BooleanHasfocus) {//TODO auto-generated Method Stubv.setvisibility (view.visible);}});} }

With these five steps in mind, a spinner is OK, and when you set the menu style for the spinner adapter, we can customize our own style and use Android directly if it's too much trouble.  R.layout, just like the following. Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);

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.