I. Achieve the following
The drop-down box in Android is a spinner component, such as a slice
Two. Implementing the Code
The layout code is as follows
<?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" > <SpinnerAndroid:id= "@+id/ddlcity"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" /> </LinearLayout>
Set up a data source
Public classMainactivityextendsActivity {PrivateString[] Items=Newstring[]{"Beijing", "Shanghai", "Guangzhou", "Changsha"}; PrivateAnimation myanimation=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Arrayadapter<String> source=NewArrayadapter<string> ( This, Android. R.layout.simple_spinner_item, items); Spinner ddlcity=(Spinner) Findviewbyid (r.id.ddlcity); Ddlcity.setadapter (source); Ddlcity.setonitemselectedlistener (NewSpinner.onitemselectedlistener () {@Override Public voidOnitemselected (adapterview<?>arg0, View arg1,intArg2,LongArg3) { NewAlertdialog.builder (mainactivity. This). Settitle (Prompted). Setmessage ("You have selected" +items[arg2]). Setpositivebutton ("OK",NULL). Show (); } @Override Public voidOnnothingselected (adapterview<?>arg0) { } }); }}Data source Setup code
The spinner data source uses Arrayadapter
arrayadapter<string> source=New arrayadapter<string> (this, Android. R.layout.simple_spinner_item, items);
You need to make a layout file for the drop-down box item, where you can choose the layout file that comes with your system.
android--drop-down box