Figure:
Introduce several methods:
1.setAdapter ()
Use the Spinner drop-down list to set the adapter. such as: Arrayadapter and so on.
2.setPrompt ()
Used to set the caption for the spinner drop-down list, with the argument string. (displayed only in the dialog style, as shown on the left, no longer in the drop-down style, as on the right)
3. Setpromptid ()
Use the Spinner drop-down list to set the caption, which is the ID of the string, such as r.string.xxxx.
4.setOnItemSelectedListener ()
Sets the listener for the spinner drop-down list.
Attach a small example:
Mainactivity.java
public class Mainactivity extends Activity {Spinner Spinner; Arrayadapter<string> Arrayadapter; string[] str = {"1", "2", "3"}, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedinstanc EState); Setcontentview (r.layout.activity_main); spinner = (spinner) Findviewbyid (R.ID.SPI); arrayadapter = new Arrayadapter<string> (Getapplicationcontext (), Android. R.layout.simple_spinner_item, str);//Spinner.setprompt ("test"); Spinner.setpromptid (r.string.spinner_id); Arrayadapter.setdropdownviewresource (Android. R.layout.simple_spinner_item); Spinner.setadapter (Arrayadapter); Spinner.setonitemselectedlistener (New ItemClick ( ));} Class ItemClick implements Onitemselectedlistener {@Overridepublic void onitemselected (adapterview<?> parent, View arg1, int arg2,long arg3) {toast.maketext (Mainactivity.this, "clicked" + parent.getitematposition (arg2), toast.length _short). Show ();} @Overridepublic void onnothingselected (adapterview<?> arg0) {}}}
Activity_main.xml
<linearlayout 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 " android:orientation= "vertical" tools:context= "${relativepackage}.${activityclass}" > <!-- Android:spinnermode= "" Drop -down menu display style- <spinner android:id= "@+id/spi" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:spinnermode= "dropdown"/></ Linearlayout>
Small instances, effects
Andorid development using the spinner control