A single menu is used as a menu, similar to the select tag in HTML. A dialog box is displayed, showing several options for selection. The screen size of the mobile phone is limited. If you use the radiogroup single-choice button, it will occupy a lot of space. The final results of today's example are as follows:
The spinner needs to bind an adapter arrayadapter and place the menu items in the adapter,
To add or delete a menu item, you only need to call the add or remove method of the adapter.
Layout XML;
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Edittext Android: Id = "@ + ID/ET"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
<Button Android: Id = "@ + ID/Add"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "add"
/>
<Button Android: Id = "@ + ID/remove"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "delete"
/>
<Spinner Android: Id = "@ + ID/SP"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</Linearlayout>
An edittext that defines the menu items to be added or deleted, an Add button, a delete button, and a spinner.
Define an initial array in strings. XML, that is, the project displayed by the spinner at the beginning. Of course, it can also be defined directly in Java code.
<String-array name = "action">
<Item> meal </item>
<Item> sleeping </item>
<Item> surfing the Internet </item>
</String-array>
Java program code:
Package com. pocketdigi. Spanner;
Import java. util. arraylist;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. arrayadapter;
Import Android. widget. Button;
Import Android. widget. edittext;
Import Android. widget. spinner;
Publicclass main extends activity {
/** Called when the activity is first created .*/
Edittext et;
Button add, remove;
Spinner sp;
Arraylist <string> List = new arraylist <string> ();
Arrayadapter <string> adapter;
@ Override
Publicvoid oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
ET = (edittext) findviewbyid (R. Id. Et );
Add = (button) findviewbyid (R. Id. Add );
Remove = (button) findviewbyid (R. Id. Remove );
SP = (spinner) findviewbyid (R. Id. SP );
// Obtain the corresponding object
String [] ls = getresources (). getstringarray (R. array. action );
// Obtain the array defined in XML
For (INT I = 0; I <ls. length; I ++ ){
List. Add (LS [I]);
}
// Import the array to arraylist
Adapter = new arrayadapter <string> (this, Android. R. layout. simple_spinner_item, list );
Adapter. setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );
// Set the drop-down menu style
Sp. setadapter (adapter );
// Bind the adapter
Sp. setprompt ("title bar ");
// Set the title bar of the dialog box
Add. setonclicklistener (New onclicklistener () {// Add button listener
@ Override
Publicvoid onclick (view v ){
// Todo auto-generated method stub
Adapter. Add (ET. gettext (). tostring ());
// Add the input item. After adding, the system automatically calls yydatasetchanged ()
// If you need to specify the position, use the insert (string S, int index) method.
Settitle (string. valueof (list. Size ()));
// The size of the list after the title output is added
}
});
Remove. setonclicklistener (New onclicklistener () {// delete button listener
@ Override
Publicvoid onclick (view v ){
// Todo auto-generated method stub
Adapter. Remove (sp. getselecteditem (). tostring ());
// Delete the selected item. After removing the item, the system automatically calls yydatasetchanged ()
Settitle (string. valueof (list. Size ()));
}
});
}
}
From: http://www.pocketdigi.com/20100810/20.html