The context menu of the Android menu ContextMenu

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.