The so-called context menu is the menu that pops up when we press a file long
To manipulate this menu we are going to rewrite the oncreatecontextmenu () method
As in the previous article, there are two ways to add dynamic additions and XML files for this menu-selected operation.
The first is dynamically added
@Override public void Oncreatecontextmenu (ContextMenu menu, View V, contextmenu.contextmenuinfo menuinfo) { Super.oncreatecontextmenu (menu, V, menuinfo); Menu.setheadertitle ("file operation"); The title of the context menu Menu.setheadericon (r.mipmap.ic_launcher);//Icon menu.add (1,1,1, "copy"); Menu.add (1,2,1, "paste"); Menu.add (1,3,1, "cut"); Menu.add (1,4,1, "renaming"); }
Then the XML file is added
@Override public void Oncreatecontextmenu (ContextMenu menu, View V, contextmenu.contextmenuinfo menuinfo) { Super.oncreatecontextmenu (menu, V, menuinfo); Menu.setheadertitle ("file operation"); The title of the context menu is Menu.setheadericon (r.mipmap.ic_launcher);/ /icon Menuinflater infalter = Getmenuinflater (); infalter.inflate (R.menu.main,menu);//Instantiate main layout }
Layout of XML files
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/menu_ Item1 " android:orderincategory=" <!-- decide the order of the items in the menu the higher the priority, the lower the precedence, the more the row is back-to- android:showasaction = "Never" <!-- Indicates whether it is displayed by default- android:title= "copy" ></item> <item android:id= "@+id/menu_item2" android:orderincategory= "android:showasaction=" " never" android:title= "Paste" ></item> <item android:id= "@+id/menu_item3" android:orderincategory= " Android:showasaction= "Never" android:title= "cut" ></item> <item android:id= "@+id/menu _item4 " android:orderincategory=" android:showasaction= " Never" android:title= " rename" ></ Item></menu>
Listening to ContextMenu.
@Override Public Boolean oncontextitemselected (MenuItem item) { switch (Item.getitemid ()) { case R.ID.MENU_ITEM1: toast.maketext (Mainactivity.this, "you chose to copy", Toast.length_short). Show (); break; Case R.ID.MENU_ITEM2: toast.maketext (Mainactivity.this, "You have selected paste", Toast.length_short). Show (); break; Case R.ID.MENU_ITEM3: toast.maketext (Mainactivity.this, "You have selected cut", Toast.length_short). Show (); break; Case R.ID.MENU_ITEM4: toast.maketext (Mainactivity.this, "You have chosen to rename", Toast.length_short). Show (); break; } return super.oncontextitemselected (item); }
The context menu of the Android menu ContextMenu