The Code has not been sorted out yet.
Package com. mediatek. uidemo;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. contextmenu;
Import Android. View. contextmenu. contextmenuinfo;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. View. submenu;
Import Android. View. view;
Import Android. widget. Button;
Import Android. widget. textview;
Import Android. widget. Toast;
Public class menuactivity extends activity {
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
This. setcontentview (R. layout. Menu );
Button menubutton1 = (button) findviewbyid (R. Id. contextmenubutton1 );
This. registerforcontextmenu (menubutton1 );
Button menubutton2 = (button) findviewbyid (R. Id. contextmenubutton2 );
This. registerforcontextmenu (menubutton2 );
}
@ Override
Public Boolean oncreateoptionsmenu (menu ){
// Todo auto-generated method stub
Menu. Add (1, 1, 1, "111 first"). setonmenuitemclicklistener (New menuitemclicklistener ());
Menu. Add (1, 2, 2, "122 second ");
Menu. Add (1, 3, 3, "211 third ");
Menu. Add (1, 4, 4, "311 forth ");
Submenu = menu. addsubmenu (2, 1, 1, "Peach ");
Submenu. Add (2,101,103, "Peach ");
Submenu. Add (2,102,102, "Peach"). setonmenuitemclicklistener (New menuitemclicklistener ());
Return super. oncreateoptionsmenu (menu );
}
Public class menuitemclicklistener implements menuitem. onmenuitemclicklistener {
@ Override
Public Boolean onmenuitemclick (menuitem item ){
Toast. maketext (menuactivity. This, item. gettitle () + "is selected", Toast. length_short). Show ();
Return true; // true indicates that the click processing of the current item is completed and will not be passed to the parent class for processing}
}
}
@ Override
Public void oncreatecontextmenu (contextmenu menu, view V,
Contextmenuinfo menuinfo ){
// Todo auto-generated method stub
If (V. GETID () = R. Id. contextmenubutton1 ){
Menu. setheadertitle ("button1 menu ");
Menu. Add (300,300,300, "context menu1"). setonmenuitemclicklistener (New menuitemclicklistener ());
Menu. Add (300,301,301, "context menu2"). setonmenuitemclicklistener (New menuitemclicklistener ());
Submenu = menu. addsubmenu (2, 1, 1, "Peach ");
Submenu. Add (2,101,103, "Peach"). setonmenuitemclicklistener (New menuitemclicklistener ());
Submenu. Add (2,102,102, "Peach"). setonmenuitemclicklistener (New menuitemclicklistener ());
} Else {
Menu. setheadertitle ("button2 menu ");
Menu. Add (301,300,300, "context men21"). setonmenuitemclicklistener (New menuitemclicklistener ());
Menu. Add (301,301,301, "context menu22"). setonmenuitemclicklistener (New menuitemclicklistener ());
}
Super. oncreatecontextmenu (menu, V, menuinfo );
}
@ Override
Public Boolean onmenuitemselected (INT featureid, menuitem item ){
// Todo auto-generated method stub
Textview TV = (textview) findviewbyid (R. Id. menutextview2 );
Switch (item. getitemid ()){
Case 4:
Toast. maketext (menuactivity. This, item. gettitle () + "is selected, item ID is 4! ", Toast. length_long). Show ();
Break;
Case 400:
TV. settext ("another UI ");
Break;
Case 401:
TV. settext ("menu UI ");
Break;
Default:
Toast. maketext (menuactivity. This, item. gettitle () + "is selected, item ID is not 4 !! ", Toast. length_long). Show ();
Break;
}
Return super. onmenuitemselected (featureid, item );
}
@ Override
Public Boolean onprepareoptionsmenu (menu ){
// Todo auto-generated method stub
Textview TV = (textview) findviewbyid (R. Id. menutextview2 );
String currenttext = TV. gettext (). tostring ();
If ("menu UI". Equals (currenttext )){
Menu. Clear (); // clear the menu first
Menuitem item = menu. Add (0,400,401, "to another UI ");
// You can click this menu item to change the value of TV1 so that (to N) the test can be performed.
Item. seticon (Android. R. drawable. alert_dark_frame); // the built-in Android icon
}
If ("another UI". Equals (currenttext )){
Menu. Clear (); // clear the menu first
Menuitem item = menu. Add (0,401,402, "to menu UI ");
// You can click this menu item to change the value of TV1 so that (to m) the test can be performed.
Item. seticon (Android. R. drawable. alert_light_frame );
}
Menu. Add (0,402,403, "now is" + currenttext );
// There are currently two menu items
Return super. onprepareoptionsmenu (menu );
}
}