Override onprepareoptionsmenu (menu) is very easy to use. We can define a Boolean value for determination. If conditions are met, we can dynamically set the menu bar to certain States, such as whether it is visible, text value.
For example, I set a Boolean value addingnew = flase, and then assign a value to addingnew in other methods. In override onprepareoptionsmenu (menu), we determine addingnew. If its value is true, the menu item whose ID is remove_todo is set to text = "cancel ". Otherwise, the menu item whose ID is remove_todo is set to text = "Remove ".
In short, we can make some judgments and dynamically set the menu item status.
In addition, we should accumulate experience and learn to use boolean values well, which is useful in many places.
The following is my program, which can summarize many experiences.
Public class todolist extends activity {
/** Called when the activity is first created .*/
Static final private int add_new_todo = menu. first;
Static final private int remove_todo = menu. First + 1;
Private Boolean addingnew = false; // set this value for control
Private listview mylistview;
Private edittext myedittext;
Private arraylist <string> todoitems;
Private arrayadapter <string> AA;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mylistview = (listview) findviewbyid (R. Id. mylistview );
Myedittext = (edittext) findviewbyid (R. Id. myedittext );
Todoitems = new arraylist <string> ();
Int resid = R. layout. todolist;
AA = new arrayadapter <string> (this, resid, todoitems );
Mylistview. setadapter (AA );
Myedittext. setonkeylistener (New onkeylistener (){
@ Override
Public Boolean onkey (view V, int keycode, keyevent event ){
// Todo auto-generated method stub
If (event. getaction () = keyevent. action_down ){
If (keycode = keyevent. keycode_dpad_center ){
Todoitems. Add (0, myedittext. gettext (). tostring ());
AA. notifydatasetchanged ();
Myedittext. settext ("");
Canceladd (); // The initial remove_todo menu item is invisible, and myedittext is invisible.
Return true;
}
}
Return false;
}});
Registerforcontextmenu (mylistview); // you can specify mylistview as the receiver of A contextmenu.
}
Public Boolean oncreateoptionsmenu (menu ){
Super. oncreateoptionsmenu (menu );
Menuitem itemadd = menu. Add (0, add_new_todo, menu. None, R. String. add_new );
Menuitem itemrem = menu. Add (0, remove_todo, menu. None, R. String. Remove );
Itemadd. seticon (R. drawable. add_new_item );
Itemrem. seticon (R. drawable. remove_item );
Itemadd. setshortcut ('0', 'A ');
Itemrem. setshortcut ('1', 'R ');
Return true;
}
Public void oncreatecontextmenu (contextmenu menu, view V, contextmenu. contextmenuinfo menuinfo ){
Super. oncreatecontextmenu (menu, V, menuinfo );
Menu. setheadertitle ("selected to do item ");
Menu. Add (0, remove_todo, menu. None, R. String. Remove );
} // Create a contextmenu
@ Override
Public Boolean onprepareoptionsmenu (menu ){
Super. onprepareoptionsmenu (menu );
Int idx = mylistview. getselecteditemposition ();
// Determine the value of remove_todo text by addingnew.
String removetitle = getstring (addingnew? R. String. Cancel: R. String. Remove );
Menuitem removeitem = menu. finditem (remove_todo );
Removeitem. settitle (removetitle );
// The remove_todo menu item is visible only when addingnew = true or listview is selected.
Removeitem. setvisible (addingnew | idx>-1 );
Return true;
}
@ Override
Public Boolean onoptionsitemselected (menuitem item ){
Super. onoptionsitemselected (item );
Int Index = mylistview. getselecteditemposition ();
Switch (item. getitemid ()){
Case (remove_todo ):{
// At this time, the remove_todo menu text may have two states: 1. Cancel 2. Remove
If (addingnew ){
// When addingnew = true, the text of the remove_todo menu is "cancel". In this case, cancel the operation.
Canceladd ();
} Else {
Removeitem (INDEX );
// When addingnew = false, the text of the remove_todo menu is "remov". In this case, the corresponding delete operation is executed.
}
Return true;
}
Case (add_new_todo ):{
Addnewitem ();
Return true;
}
}
Return false;
}
@ Override
Public Boolean oncontextitemselected (menuitem item ){
Super. oncontextitemselected (item );
Switch (item. getitemid ()){
Case (remove_todo ):{
Adapterview. adaptercontextmenuinfo menuinfo;
Menuinfo = (adapterview. adaptercontextmenuinfo) item. getmenuinfo ();
Int Index = menuinfo. position;
Removeitem (INDEX );
Return true;
}
}
Return false;
}
// Execute the corresponding work in the following three methods and control addingnew
Private void addnewitem (){
// Todo auto-generated method stub]
Addingnew = true;
Myedittext. setvisibility (view. Visible );
Myedittext. requestfocus ();
}
Private void removeitem (INT _ index ){
// Todo auto-generated method stub
Todoitems. Remove (_ index );
AA. notifydatasetchanged ();
}
Private void canceladd (){
// Todo auto-generated method stub
Addingnew = false;
Myedittext. setvisibility (view. Gone );
}
}