Two types of menus for Android

Source: Internet
Author: User

Android submenu and Options menu with context menu implementation

Menus are essential for Android development, but how do you implement a variety of menus? Isn't it a headache? Let me introduce you to the following:

1. Implementation of the Options menu and sub-menu

Options Menu: The most general menu, which is called option menu in Android. When the Options menu can only display 6 menu items and more than 6, the 6th menu item will be replaced by the system with a submenu called "More", which will not show up as a submenu item for the "More" menu.

submenu: Clicking on the submenu in Android will pop up the hover window to display the submenu item. The submenu does not support nesting, which means that no more submenus can be included in the sub-menu.                     

Step 1: generate submenus and options menus. Create an Android Project named "MyMenu". In the Mymenuactivity class, right-click Source→override/implement Methods. Using the Oncreateoptionsmenu () method to generate the Options menu, the code is as follows:

    public boolean Oncreateoptionsmenu (Menu menu) {   //Build Options Menu        //TODO auto-generated method stub        Super.oncreateoptionsmenu (menu);    Call the parent class method to join the system menu        menu.add (1,1,1, "share");             Menu.add (1,2,2, "cut");             Add Menu item        menu.add (1,3,3, "delete");        Menu.add (1,4,4, "edit");        Menu.add (1,5,5, "rotation");        Menu.add (1,6,6, "callout");        Menu.add (1,7,7, "more Information");       Menu.add (int groupId, int itemId, int order, charsequence title);        Menu.add (1,8,8, "Set as Wallpaper");        Submenu Submenu=menu.addsubmenu ("System Settings"),//Can be the menu item        submenu.add (1,1,1, "Display Settings");        Submenu.add (1,2,2, "Advanced Settings");        Submenu.add (1,3,3, "Network Settings");        Submenu.add (1,4,4, "Security Settings");        return true;    }

Step 2: response menu. onoptionsitemselected (MenuItem), whenever a menu item is clicked, Android invokes the method and passes in the clicked menu item.

public boolean onoptionsitemselected (MenuItem item) {   //Response menu        //TODO auto-generated method stub        B1.settext (Item.gettitle ());   Which menu item is clicked, B1 displays the title of which menu item,        return true;    

2. Implementation of the context menu

Context Menu: The menu that appears after the Long press of the view control in Android, Windows right-click popup menu is the context menu.

Step 1: Register the context menu

public class Mymenuactivity extends activity {    /** called when the activity is first created. */    @Override    p ublic void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.main);        b1= (Button) Findviewbyid (R.ID.B1);   Registration button B1        This.registerforcontextmenu (B1);   Register context Menu    }    Button B1;

Step 2: Build the context Menu

public void Oncreatecontextmenu (ContextMenu menu, View V,    //Generate context menu            Contextmenuinfo menuinfo) {        //TODO auto-generated method Stub        if (V.getid () ==r.id.b1)        {            menu.clear ();     Clears the context menu (some default menu items)            menu.setheadertitle ("file operation");   Context Menu Title            menu.add (0, 1, menu.none, "send");     Add context menu item            menu.add (0, 2, Menu.none, "mark as important");            Menu.add (0, 3, menu.none, "renaming");            Menu.add (0, 4, menu.none, "delete");        }    }

Step 2: Response context Menu

    public boolean oncontextitemselected (MenuItem item) {  //Response context menu        //TODO auto-generated method stub        T1.settext (Item.gettitle ());        return true;    }

Sentiment

I felt full before I wrote it and thought it was clear. But when it comes to writing, ideas get messy (maybe I'm not concentrating). However, it is finished, there is a wrong to write the local pro must point out to Oh!

http://blog.csdn.net/dyllove98/article/details/8841769

 

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.