I. Overview
In Android, the context menu is through Onlongclick (...) event is accessed. Displays the menu item after the event is triggered.
When using context menus, usually in OnCreate (...) method, register the context menu in advance. In the implementation of Oncreatecontextmenu (...) Methods and oncontextitemselected (...) Method.
Registration Menu Method: Registerforcontextmenu (...)
Logout Registration Menu Method: Unregisterforcontextmenu (...)
Ii. Adding a context menu using an XML file
Text_menu.xml context menu options:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Menuxmlns:android= "Http://schemas.android.com/apk/res/android">3 <ItemAndroid:title= "File">4 <Menu>5 <GroupAndroid:checkablebehavior= "None">6 <ItemAndroid:id= "@+id/file_new"7 Android:title= "New"8 Android:icon= "@android:d rawable/ic_input_add"9 Android:alphabeticshortcut= "n" />Ten <ItemAndroid:id= "@+id/file_save" One Android:title= "Save" A Android:icon= "@android:d rawable/ic_menu_save" - Android:alphabeticshortcut= "S" /> - </Group> the </Menu> - </Item> - <ItemAndroid:title= "Edit"> - <Menu> + <!--radio Button Group - - <GroupAndroid:checkablebehavior= "single"> + <ItemAndroid:id= "@+id/cut" A Android:title= "Cut" at Android:alphabeticshortcut= "Ctrl + x" /> - <ItemAndroid:id= "@+id/copy" - Android:title= "Copy" - Android:alphabeticshortcut= "Ctrl + C" /> - <ItemAndroid:id= "@+id/paste" - Android:title= "Paste" in Android:alphabeticshortcut= "Ctrl + V" /> - </Group> to </Menu> + </Item> - </Menu>
In code, add a context menu display, and trigger an event when a context menu item is selected (Mainactivity.java):
1 Public classMainactivityextendsActivity2 {3 Private Static FinalString TAG = "Mainactivity";4 PrivateContext Mcontext;5 PrivateEditText Meditmenu;6 PrivateMenuinflater Mmenuinflater;7 8 @Override9 Public voidonCreate (Bundle savedinstancestate)Ten { One Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); - -Mcontext =Getapplicationcontext (); the - //binding menu Button Events -Meditmenu =(EditText) Findviewbyid (R.ID.INPUT_TEXT3); - //Register context Menu + Registerforcontextmenu (meditmenu); - } + A //context menu, binding with EditView control, long Press to display menu item at @Override - Public voidOncreatecontextmenu (ContextMenu menu, view view, Contextmenu.contextmenuinfo Contextmenuinfo) - { - Super. Oncreatecontextmenu (menu, view, contextmenuinfo); - //Create a new ContextMenu object -Mmenuinflater =NewMenuinflater ( This); in //Initializes a new ContextMenu object using XML with the original menu object - mmenuinflater.inflate (R.menu.text_menu, menu); to } + - //context menu, triggering context menu item response when clicking an event the @Override * Public Booleanoncontextitemselected (MenuItem Item) $ {Panax Notoginseng Switch(Item.getitemid ()) - { the Caser.id.file_new: + { AToast.maketext (Mcontext, "File new."), Toast.length_short). Show (); the Break; + } - CaseR.id.file_save: $ { $Toast.maketext (Mcontext, "File save."), Toast.length_short). Show (); - Break; - } the CaseR.id.cut: - {WuyiToast.maketext (Mcontext, "Edit cut."), Toast.length_short). Show (); the Break; - } Wu Caser.id.copy: - { AboutToast.maketext (Mcontext, "Edit copy."), Toast.length_short). Show (); $ Break; - } - CaseR.id.paste: - { AToast.maketext (Mcontext, "Edit paste."), Toast.length_short). Show (); + Break; the } - default: $Toast.maketext (Mcontext, "no response", Toast.length_short). Show (); the } the return true; the } the - //Unregister a registered context menu in @Override the Public voidOnDestroy () the { About Super. OnDestroy (); the Unregisterforcontextmenu (meditmenu); the } the}
When any of the Ps:contextmenu items is selected, the entire contextmenu is automatically shut down by the system, even if multiple options are true. In this case, the multi-option behavior is somewhat bizarre. After viewing the Android source, it is an Android bug, it should be the case that Google does not consider multiple options.
Although the owner of the context menu is View, the context menu is generated through the activity's Oncreatecontextmenu (ContextMenu menu, view V,contextmenu.contextmenuinfo Menuinfo) method, which is much like the Oncreateoptionsmenu (Menu menu) method for generating Options menu. The difference is that Oncreateoptionsmenu is called only when the user presses the "menu" key for the first time , and Oncreatecontextmenu is called every time the user presses the view. And the view must already have the context menu registered.
Android context Menu