>> ContextMenu is the context menu for Android, which is displayed when you select a view and press and hold the menu key. For example, Editetext can use long press to pop up with "cut", "copy", "Paste" and other items of ContextMenu.
To implement ContextMenu, the following three methods are generally used:
(1) Registerforcontextmenu (Getexpandablelistview ());
(2) Oncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo menuinfo);
(3) oncontextitemselected (MenuItem item);
The main difference between >>contextmenu and Optionsmenu is that 1,contextmenu must be registered through the activity's Registerforcontextmenu (View), and Optionsmenu not. 2,contextmenu does not support icon, and Optionsmenu supports it. 3,contextmenu can have a head, can be setheadericon,setheadertitle,setheaderview to set the head, otherwise there is no head. 4, The way it pops up is different. 5,the owner of the Options menu is activity, and the owner of the context menu is the view in the activity。 Each activity has and has only one options Menu, which serves the entire activity. While an activity often has multiple view, not every view has a context menu, which requires ourcalledregisterforcontextmenu (view view) to specify . Note: If any of the ContextMenu items are selected, the entire ContextMenu will be 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.
Another notable thing is the contextmenuinfoin which the object of this class is passed into the oncreatecontextmenu (ContextMenu menu, View V, Contextmenu.contextmenuinfo menuinfo) method, then what's the use of it? Sometimes, a view element needs to pass some information to the context menu, such as the ID of the view corresponding to the DB record, and this will use Contextmenuinfo. The view that needs to pass additional information needs to override the Getcontextmenuinfo () method to return a Contextmenuinfo implementation class object with data.
Introducing so much, here's a demo showing how to create and respond to context menus:
1. In the activity of the OnCreate (...) method to register the context menu for a view
2. In Oncreatecontextmenuinfo (...) In the build context menu.
3. In oncontextitemselected (...) The context menu item in response.
Menu
1) Register context menu
/**
* Context Menu Demo Demo
*
* @author Codingmyworld 2011-8-27 03:22:39
*/
Publicclass Samplecontextmenuactivity extends Listactivity {
privatestaticfinal String TAG = "samplecontextmenuactivity";
@Override
Protectedvoid onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Display list
Simpleshowlist ();
Registering a context menu for all list items
This.registerforcontextmenu (Getlistview ());
}
Privatevoid Simpleshowlist () {
List item
string[] Files =new string[] {
"File 1",
"File 2",
"File 3",
"File 4"
};
Simple Array Adapter
Arrayadapter<string> Adapter =new arrayadapter<string> (
This
Android. R.layout.simple_list_item_1,
files);
Set Adapter
This.setlistadapter (adapter);
LOG.V (TAG, "Show Simple List");
}
}
2) Generate context Menu
Override the method in the activity.
@Override
Publicvoid Oncreatecontextmenu (ContextMenu menu, View V,
Contextmenuinfo menuinfo) {
LOG.V (TAG, "Populate context menu");
Set context menu Title
Menu.setheadertitle ("file operation");
Add context menu item
Menu.add (0, 1, menu.none, "send");
Menu.add (0, 2, Menu.none, "marked as important");
Menu.add (0, 3, menu.none, "renaming");
Menu.add (0, 4, menu.none, "delete");
}
3) Response Context menu item
Similar to the Response Options menu, the only difference is that additional information can be obtained through menu info.
@Override
Publicboolean oncontextitemselected (MenuItem item) {
Get the item information that is currently selected
Adaptercontextmenuinfo Menuinfo = (adaptercontextmenuinfo) item.getmenuinfo ();
LOG.V (TAG, "Context item seleted id=" + menuinfo.id);
Switch (Item.getitemid ()) {
CASE1:
Do something
Break
CASE2:
Do something
Break
CASE3:
Do something
Break
CASE4:
Do something
Break
Default
returnsuper.oncontextitemselected (item);
}
Returntrue;
}
Run the program, call out the context menu several times, notice the output of Logcat, Oncreatecontextmenu is called every time.
How to use Android ContextMenu and how it differs from Optionmenu (RPM)