The spinner is equivalent to the drop-down list box in HTML. In the following example, we will learn how to use the spinner.
Attribute:
Attribute name |
Description |
Android: Prompt |
This prompt is displayed in the drop-down list dialog box. (Translator's note: The title of the dialog box: |
Method:
Public charsequence getprompt ()
Return Value
Prompt displayed when the dialog box is displayed (obtain the title word in the pop-up view)
Public void onclick (dialoginterface dialog, int which)
This method is called when you click an item in the pop-up box.
Parameters
Dialog click the pop-up dialog box
Which
Click a button or click a location.
Public Boolean
Performclick ()
If it is defined, the onclicklistener of this view is called. (for example, a drop-down list is displayed by default during loading ).
Return Value
Is true. A specified onclicklistener is called. If it is false, it is not called.
Public void setonitemclicklistener (adapterview. onitemclicklistener I)
The spinner does not support item click events. Calling this method causes an exception.
Parameters
L this listener will be ignored
Public void
Setpromptid (charsequence prompt)
Prompt displayed when the dialog box is displayed (Translator's note: Set the title word in the pop-up view)
Parameters
Prompt for prompt settings
Public void setpromptid (INT promptid)
Prompt displayed when the dialog box is displayed (Translator's note: Set the title word in the pop-up view)
Parameters
Prompted
The prompt displayed in the dialog box displays the resource ID.
Example:
Package android. imageview; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. OS. bundle; import android. widget. arrayadapter; import android. widget. spinner; public class imageview1activity extends activity {/** called when the activity is first created. */private spinner = NULL; private arrayadapter <string> adapter = NULL; private list <string> List = NULL; @ Override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); spinner = (spinner) findviewbyid (R. id. spinner); spinner. setprompt ("Please select the fruit you like? "); List = new arraylist <string> (); list. add ("apple"); list. add ("banana"); list. add ("Pear"); list. add ("Peach"); adapter = new arrayadapter <string> (imageview1activity. this, android. r. layout. simple_dropdown_item_1line, list); spinner. setadapter (adapter );}}
Another method:
Package android. imageview; import android. app. activity; import android. OS. bundle; import android. widget. arrayadapter; import android. widget. spinner; public class imageview1activity extends activity {/** called when the activity is first created. */private spinner spinner1 = NULL; private arrayadapter <charsequence> adapter = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (Savedinstancestate); setcontentview (R. layout. Main); spinner1 = (spinner) findviewbyid (R. Id. spinner1); spinner1.setprompt ("Where do you want to work in the future? "); Adapter = arrayadapter. createfromresource (this, R. array. City, Android. R. layout. simple_spinner_item); spinner1.setadapter (adapter );}}
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello world, imageview1activity! </String> <string name = "app_name"> imageview1 </string> <string-array name = "city"> <item> Shandong </item> <item> Shanghai </item> <item> Beijing </item> <item> Dalian </item> </string-array> </resources>
Loading events:
Package android. imageview; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterview; import android. widget. adapterview. onitemselectedlistener; import android. widget. arrayadapter; import android. widget. spinner; import android. widget. toast; public class imageview1activity extends activity {/** called when the activity is first created. */private spinn Er spinner1 = NULL; private arrayadapter <charsequence> adapter = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); spinner1 = (spinner) findviewbyid (R. id. spinner1); spinner1.setprompt ("Where do you want to work in the future? "); Adapter = arrayadapter. createfromresource (this, R. array. city, android. r. layout. simple_spinner_item); spinner1.setadapter (adapter); spinner1.setonitemselectedlistener (New onitemselectedlistener () {public void onitemselected (adapterview <?> Arg0, view arg1, int arg2, long arg3) {// todo auto-generated method stub toast. maketext (imageview1activity. this, "the selected city is:" + spinner1.getitematposition (arg2 ). tostring (), toast. length_long ). show ();} public void onnothingselected (adapterview <?> Arg0) {// todo auto-generated method stub }});}}