From the previous lessons, we can see that listview is used to display a long list and occupy the entire screen (listactivity ). However, sometimes you may need other similar views, so that you do not have to fill the entire screen. In this case, you should use the spinner control. The spinner displays a piece of information in the list at a time and allows you to select.
The following describes how to use a spinner in an activity.
1. Create a project: basicviews6.
2. In Main. XMLCode.
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <spinner Android: id = "@ + ID/spinner1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: drawselectid Top = "true"/> </linearlayout>
3. Code in strings. xml.
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello world, basicviews6activity! </String> <string name = "app_name"> basicviews6 </string> <string-array name = "presidents_array"> <item> Dwight D. eisenhower </item> <item> John F. kennedy </item> <item> ly1_ B. johnson </item> <item> Richard Nixon </item> <item> Gerald Ford </item> <item> Jimmy Carter </item> <item> Ronald Reagan </item> <item> George H. w. bush </item> <item> Bill Clinton </item> <item> George W. bush </item> <item> Barack Obama </item> </string-array> </resources>
4. The code in basicviews6activity. java.
public class basicviews6activity extends activity {string [] presidents;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); presidents = getresources (). getstringarray (R. array. presidents_array); spinner S1 = (spinner) findviewbyid (R. id. spinner1); arrayadap TER
adapter = new arrayadapter
(this, android. r. layout. simple_spinner_item, Presidents); s1.setadapter (adapter); s1.setonitemselectedlistener (New onitemselectedlistener () {@ overridepublic void onitemselected (adapterview
Arg0, view arg1, int arg2, long arg3) {int Index = arg0.getselecteditemposition (); toast. maketext (getbasecontext (), "you have selected item:" + Presidents [Index], toast. length_short ). show () ;}@ overridepublic void onnothingselected (adapterview
Arg0) {}}) ;}
5. Press F11 to debug the simulator. Click on the spinner and you will see a pop-up window showing the names. If you select a name, a message is displayed.
This example is similar to listview. The method you need to implement is onnothingselected (). When you press the return key, this method is triggered and the displayed window is canceled. In this example, we didn't do anything, so we didn't implement this method.
In addition to displaying some simple lists, you can also display radiobutton. You only need to modify the second parameter of arrayadapter.
Arrayadapter <string> adapter = new arrayadapter <string> (this, Android. R. layout. simple_list_item_single_choice, Presidents );