Example The previous example has a rough idea about the design method of the custom menu and interactive events of the spinner. However, to dynamically increase or decrease the menu options of the spinner drop-down menu in the Android system, the arraylist dependency must be used. In the following example, an edittext is designed. When the user inputs a new text, when the "add" button is clicked, the input value is added to the spinner (to the last entry in the drop-down menu), and the spinner stays on the added option. When you click the delete button, delete the selected spinner option, which is often applied to the to-do list with an unknown number of spinner options or to add and maintain city and county data. <! -- [Endif] --> ▲Figure 4-9 a menu with user input that can be dynamically added/deleted Sample program Src/IRDC. ex04_09/ex04_09.java The onitemselectedlistener event is added to the spinner. When you click the drop-down menu, the value is taken to the text-view on the top. In the previous example, a string array is input when the new adapter is added. This time, arraylist is required because the adapter is to be added and deleted. Otherwise, an error occurs when adding or deleting the adapter. /* Import program omitted */ Public class ex04_09 extends Activity { Private Static final string [] countriesstr = {"Beijing", "Shanghai", "Tianjin", "Chongqing "}; Private textview mytextview; Private edittext myedittext; Private button mybutton_add; Private button mybutton_remove; Private spinner myspinner; Private arrayadapter <string> adapter; Private list <string> allcountries; /** Called when the activity is first created .*/ @ Override Public void oncreate (bundle savedinstancestate) { Super. oncreate (savedinstancestate ); /* Load main. xml Layout */ Setcontentview (R. layout. Main ); Allcountries = newArraylist<String> (); For (INT I = 0; I <countriesstr. length; I ++) { Allcountries. Add (countriesstr [I]); } /* New arrayadapter object and pass allcountries */ Adapter = new arrayadapter <string> (this, Android. R. layout. simple_spinner_item, allcountries ); Adapter . Setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item ); /* Get the object with findviewbyid */ Mytextview = (textview) findviewbyid (R. Id. mytextview ); Myedittext = (edittext) findviewbyid (R. Id. myedittext ); Mybutton_add = (button) findviewbyid (R. Id. mybutton_add ); Mybutton_remove = (button) findviewbyid (R. Id. mybutton_remove ); Myspinner = (spinner) findviewbyid (R. Id. myspinner ); /* Add the arrayadapter to the spinner object */ Myspinner. setadapter (adapter ); /* Add onclicklistener to mybutton_add */ Mybutton_add.setonclicklistener (New button. onclicklistener () { @ Override Public void onclick (view arg0) { String newcountry = myedittext. gettext (). tostring (); /* Compare whether the added value already exists and does not exist before adding it */ For (INT I = 0; I <Adapter. getcount (); I ++) { If (newcountry. Equals (adapter. getitem (I ))) { Return; } } If (! Newcountry. Equals ("")) { /* Add the value to the adapter */ Adapter. Add (newcountry ); /* Obtain the position of the added value */ Int position =Adapter. getposition (newcountry); /* Select the position of the spinner in the added value */ Myspinner. setselection (position ); /* Clear myedittext */ Myedittext. settext (""); } } }); /* Add onclicklistener to mybutton_remove */ Mybutton_remove.setonclicklistener (New button. onclicklistener () { @ Override Public void onclick (view arg0) { If (myspinner. getselecteditem ()! = NULL) { /* Delete the value of myspinner */ Adapter. Remove (myspinner. getselecteditem (). tostring ()); /* Clear myedittext */ Myedittext. settext (""); If (adapter. getcount () = 0) { /* Clear mytextview */ Mytextview. settext (""); } } } }); /* Add onitemselectedlistener to myspinner */ Myspinner. setonitemselectedlistener (New Spinner. onitemselectedlistener () { @ Override Public void onitemselected (adapterview <?> Arg0, view arg1, int arg2, Long arg3) { /* Bring the value of the selected myspinner to mytextview */ Mytextview. settext (Arg0.getselecteditem (). tostring ()); } @ Override Public void onnothingselected (adapterview <?> Arg0) { } }); } } Extended learning Setdropdownviewresource is mainly used to set the drop-down menu style that appears after the user clicks the spinner. In addition to the previous example, the custom mode is used to change the textview content, Android also provides two basic styles: · Android. R. layout. simple_spinner_item: drop-down menu of textview; · Android. R. layout. simple_spinner_dropdown_item: In addition to textview, there is also a radio drop-down menu on the right. View simple_spinner_dropdown_item.xml In the android source code. The content is as follows: <? XML version = "1.0" encoding = "UTF-8"?> <Textview Xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ Android: ID/text1" Android: layout_width = "fill_parent" Android: layout_height = "? Android: ATTR/listpreferreditemheight" Android: singleline = "true" Style = "? Android: ATTR/spinnerdropdownitemstyle" /> After the custom modifications are made, layout applies to the spinner: <? XML version = "1.0" encoding = "UTF-8"?> <Textview Xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ Android: ID/text1" Android: layout_width = "fill_parent" Android: layout_height = "12sp" Android: singleline = "true" Style = "? Android: ATTR/spinnerdropdownitemstyle" Android: textsize = "10sp" /> |