Previous period learned the use of the GridView, you have mastered it? This issue together to learn the use of spinner.
I. Understanding Spinner
Spinner is actually a list selection box. However, the Android List selection box does not need to display a drop-down list, but is equivalent to a popup menu for the user to select.
Spinner and Gallery both inherited the Absspinner,absspinner inherited Adapterview, so it also shows Adapterview features: As long as Adapterview to provide adapter.
Common XML attributes supported by spinner and related methods are shown in the following table.
If a developer uses spinner to determine the list item in the list selection box, it is completely unnecessary to write code, as long as the Android:entries attribute is specified for spinner to allow spinner to function properly, if the program needs to be dynamically To determine the list item for spinner, or if the program needs to customize the list item for spinner, you can use adapter to provide the list item for spinner.
Ii. examples of Spinner
Next, learn how to use spinner using a simple example program.
Continue to use the Listviewsample module of the Widgetsample project to create a spinner_layout.xml file in the app/main/res/layout/directory populated with the following code snippet:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"android:padding= "5DP" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Select Professional Direction"Android:textcolor= "#44BDED"android:textsize= "18SP" /> <SpinnerAndroid:id= "@+id/spin_one"Android:layout_width= "100DP"Android:layout_height= "64DP"android:entries= "@array/professionals"Android:spinnermode= "Dialog" /> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margintop= "10DP"Android:text= "Select Textbook"Android:textcolor= "#F5684A"android:textsize= "18SP" /> <SpinnerAndroid:id= "@+id/spin_two"Android:layout_width= "Wrap_content"Android:layout_height= "64DP" /></LinearLayout>
Create a new Arrays.xml file under the res/values/directory and define the Professionals array resource as follows:
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <String-arrayname= "Professionals"> <Item>Android</Item> <Item>Java</Item> <Item>Python</Item> <Item>Php</Item> <Item>. Net</Item> <Item>C++</Item> <Item>C</Item> </String-array></Resources>
Next, provide adapter for spinner. Create a new Spinneractivity.java file and load the new layout file above, with the following code:
Packagecom.jinyu.cqkxzsxy.android.listviewsample;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.Spinner;ImportAndroid.widget.Toast; Public classSpinneractivityextendsappcompatactivityImplementsAdapterview.onitemselectedlistener {PrivateSpinner Mprospinner =NULL; PrivateSpinner Mbookspinner =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.spinner_layout); //get the Spinner component in the interface layout fileMprospinner =(Spinner) Findviewbyid (R.id.spin_one); Mbookspinner=(Spinner) Findviewbyid (r.id.spin_two); String[] Arr= {"Beginner Android Development", "Android Beginner Development", "Android Intermediate development", "Android Advanced Development", "Android Development Step"}; //Create a Arrayadapter objectarrayadapter<string> adapter =NewArrayadapter<string> ( This, Android. R.layout.simple_list_item_1, arr); //set adapter for spinnerMbookspinner.setadapter (adapter); //set selected event listeners for spinnerMprospinner.setonitemselectedlistener ( This); Mbookspinner.setonitemselectedlistener ( This); } @Override Public voidOnitemselected (adapterview<?> Parent, view view,intPositionLongID) {String content=parent.getitematposition (position). ToString (); Switch(Parent.getid ()) { CaseR.id.spin_one:toast.maketext (spinneractivity. This, "Select the major is:" +content, Toast.length_short). Show (); Break; CaseR.id.spin_two:toast.maketext (spinneractivity. This, "The selected textbook is:" +content, Toast.length_short). Show (); Break; default: Break; }} @Override Public voidOnnothingselected (adapterview<?>Adapterview) { }}
The above program is relatively simple, mainly for spinner set the selected settings listener.
Modify the program start activity, run the program, you can see the interface effect shown.
Click the first spinner, and the Select dialog box appears, as shown in. Select one of the items to return to the main interface and discover that the value of spinner will change to the selected content.
Similarly click on the second spinner, open the drop-down list options box as shown in.
Gallery and spinner components have a common parent class: Absspinner, which indicates that both gallery and spinner are a list selection box. The difference between them is that spinner displays a vertical list selection box, and gallery displays a horizontal list selection box. There is another difference between gallery and spinner: Spinner's role is for the user to choose, and Gallery allows the user to drag to view the previous and next list items.
The usage of gallery itself is very simple-basically similar to spinner's usage, as long as it provides a content adapter, the view returned by the adapter GetView () method will be the list item for the gallery list. If the program needs to monitor changes to the gallery selection, it can be done by adding a onltemselectedlistener listener to the gallery.
Android is no longer recommending the use of the gallery component, but it is recommended to use other horizontal scrolling components, such as Horizontalscrollview and Viewpager, instead of the gallery component, so there is no explanation here.
Come here today, if you have any questions welcome message to discuss together, also welcome to join the Android 0 Basic introductory Technical discussion group, grow together!
This article copyright for the public Share talent show (Shareexpert)--Xin 鱻 all, if necessary reprint please contact the author authorized, hereby declare!
Past period Summary share:
Android 0 Basics Introduction 1th: Android's past life
Android 0 Basics Section 2nd: Android system Architecture and application components those things
Android 0 Basics Section 3rd: Bring you up to talk about Android development environment
Android 0 Basics 4th: Installing and configuring the JDK correctly Ko fu the first trick
Android 0 Basics 5th: Use ADT bundles to easily meet the goddess
Android 0 Basics 6th: Configuration Optimization SDK Manager, official dating goddess
Android 0 Basics 7th: Take care of Android simulator and start the Sweet journey
Android 0 Basics 8th: HelloWorld, the starting point for my first trip
Android 0 Basics 9th: Android app, no code can be developed
Android 0 Basics Section 10th: Development IDE Big upgrade, finally ushered in Android Studio
Android 0 Basics Introductory Section 11th: Simple steps to take you to fly, run Android Studio project
Android 0 Basics 12th: Get familiar with the Android studio interface and start selling
Android 0 Basics 13th: Android Studio Configuration optimization to create a development tool
Android 0 Basics 14th: Using high-speed genymotion, stepping into the rocket era
Android 0 Basics Section 15th: Mastering the Android Studio project structure, sailing
Android 0 Basics Section 16th: Android User Interface Development overview
Android 0 Basics Section 17th: TextView Properties and Methods Daquan
Android 0 Basics Section 18th: EditText properties and how to use them
Android 0 Basics section 19th: Button usage explained
Android 0 Basics Section 20th: checkbox and RadioButton Usage Daquan
Android 0 Basics Section 21st: ToggleButton and switch usage Daquan
Android 0 Basics Section 22nd: ImageView's properties and methods Daquan
Android 0 Basics Section 23rd: ImageButton and Zoombutton use Daquan
Android 0 Basics Section 24th: Customize view simple to use to create your own controls
Android 0 Basics Section 25th: Simple and most commonly used linearlayout linear layouts
Android 0 Basics Section 26th: Two alignments, layout_gravity and gravity differ greatly
Android 0 Basics section 27th: Using padding and margin correctly
Android 0 Basics Section 28th: Easy to master relativelayout relative layout
Android 0 Basics 29th: Use tablelayout table layouts
Android 0 Basics 30th: Two minutes master Framelayout frame layout
Android 0 Basics Section 31st: absolutelayout absolute Layout with less
Android 0 Basics Section 32nd: New GridLayout Grid layout
Android 0 Basics section 33rd: Android Event Handling overview
Android 0 Basics Section 34th: Listening-based event handling in Android
Android 0 Basics Section 35th: Callback-based event handling in Android
Android 0 Basics Section 36th: Handling of Android system events
Android 0 Basics 37th: First Knowledge ListView
Android 0 Basics 38th: First Knowledge Adapter
Android 0 Basics section 39th: listactivity and custom list items
Android 0 Basics Section 40th: Customizing Arrayadapter
Android 0 Basics Section 41st: Using Simpleadapter
Android 0 Basics Section 42nd: Customizing Baseadapter
Android 0 Basics section 43rd: ListView Optimization and List End-to-end use
Android 0 Basics Section 44th: ListView Data Dynamic Update
Android 0 Basic Getting Started section 45th: Grid view GridView
Android 0 Basics 46th: drop-down box spinner