What is ContextMenu? For example, the Windows System right-click menu, Long Press a folder in Android, will jump out of the menu, this menu is ContextMenu. The difference between ContextMenu and Optionmenu 1. Optionmenu corresponds to activity, an activity can only have one option menu; 2. ContextMenu corresponding to the view, each view can be set context menu; 3. In general, ContextMenu is commonly used in ListView or GridView. The ContextMenu creation method first adds the context menu content Oncreatecontextmenu () to the View Registration context menu Registerforcontextmenu ()--which can be dynamically added by code;-- You can load a menu item in an XML file. Sets the response event oncontextitemselected () after the menu click.
1 Public classMainactivityextendsactionbaractivity {2 3 @Override4 protected voidonCreate (Bundle savedinstancestate) {5 Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.fragment_main);7 Showlistview ();8 9 }Ten One //setting Showlistview display content A Private voidShowlistview () { -ListView ListView =(ListView) Findviewbyid (r.id.listview1); -arrayadapter<string> adapter =NewArrayadapter<string> ( This, the Android. R.layout.simple_list_item_1, GetData ()); - Listview.setadapter (adapter); - This. Registerforcontextmenu (ListView); - + } - + @Override A Public voidOncreatecontextmenu (ContextMenu menu, View V, at contextmenuinfo Menuinfo) { - //TODO auto-generated Method Stub - Super. Oncreatecontextmenu (menu, V, menuinfo); - //setting Meun display content -Menu.setheadertitle ("File operation"); - Menu.setheadericon (r.drawable.ic_launcher); in //method one code load - //menu.add (1, 1, 1, "copy"); to //menu.add (1, 2, 1, "paste"); + //menu.add (1, 3, 1, "cut"); - //menu.add (1, 4, 1, "rename"); the //method Two, XML loading: *Menuinflater Inflater =Getmenuinflater (); $ inflater.inflate (R.menu.main, menu);Panax Notoginseng - } the + //Listener Events: A @Override the Public Booleanoncontextitemselected (MenuItem item) { + //TODO auto-generated Method Stub - $ Switch(Item.getitemid ()) { $ //method One code load: - Case1: -Toast.maketext (mainactivity. This, "Click to copy", 0). Show (); the Break; - Wuyi Case2: theToast.maketext (mainactivity. This, "Click to paste", 0). Show (); - Break; Wu Case3: -Toast.maketext (mainactivity. This, "Click Cut", 0). Show (); About Break; $ - Case4: -Toast.maketext (mainactivity. This, "Click Rename", 0). Show (); - Break; A //method Two, XML load + Caser.id.context_menu_item1: theToast.maketext (mainactivity. This, "Click to copy", 0). Show (); - Break; $ Caser.id.context_menu_item2: theToast.maketext (mainactivity. This, "Click to paste", 0). Show (); the Break; the Caser.id.context_menu_item3: theToast.maketext (mainactivity. This, "Click Cut", 0). Show (); - Break; in CaseR.ID.CONTEXT_MENU_ITEM4: theToast.maketext (mainactivity. This, "Click Rename", 0). Show (); the Break; About the } the return Super. oncontextitemselected (item); the } + - //construct listview Display Data the PrivateArraylist<string>GetData () {Bayi thearraylist<string> list =NewArraylist<string>(); the for(inti = 1; I < 5; i++) { -List.add ("file" +i); - } the returnlist; the } the}
Android Siege Lion knows ContextMenu