Home>

There are four types of menus in the android system:options menu, context menu, sub menu, popup menu, this article will give you a detailed explanation of the menu menu of android development, interested Let's study together

There are four types of menus in the android system:options menu, context menu, sub menu, popup menu.

First, the options menu (optionsmenu)

I. Method Introduction:

public booleanoncreateoptionsmenu (menu menu):Use this method to call optionsmenu.

public booleanonoptionsitemselected (menuitem item):The action that occurs after a menu item is selected.

public voidonoptionsmenuclosed (menu menu):Action that occurs after the menu is closed.

public booleanonprepareoptionsmenu (menu menu):the onprepareoptionsmenu method will be called before the option menu is displayed,You can use this method to adjust the menu according to the situation.

public booleanonmenuopened (int featureid, menu menu):Action that occurs after a single open.

Second, the default style

The default style is to pop up a menu at the bottom of the screen,This menu is called the options menu optionsmenu. Generally,Option menu shows up to 2 rows with 3 menu items in each row,These menu items have text and icons,Also called icon menus, if there are more than 6 items, they will be hidden from the sixth item,There will be a more in the sixth item. Clicking more will bring up the sixth item and later menu items.These menu items are also called expanded menus. Introduced below.

1. Overload oncreateoptionsmenu (menu menu) method

@override
public boolean oncreateoptionsmenu (menu menu)
{
 //inflate the menu;this adds items to the action bar if it is present.
 //getmenuinflater().inflate(r.menu.main, menu);
 menu.add (0,1,1, "setting"). seticon (r.drawable.setting);
 menu.add (0,3,3, "setting"). seticon (r.drawable.setting);
 menu.add (0,2,2, "Download"). seticon (r.drawable.download);
}

Overload oncreateoptionsmenu (menu menu) method and add menu items in this method,Finally returns true, if false, the menu will not be displayed.

note:

Most mobile phones will have a "menu" button. After an application is installed on the phone,The menu associated with the application can be displayed via "menu".

However, starting with android 3.0, android no longer requires menu cases to be provided on mobile devices. Although many mobile phones now provide menu buttons, some are no longer provided.under these circumstances,Android recommends using actionbar instead of menu.In a future blog post, we will introduce Android's support for actionbar.

The optionsmenu on 4.2 is placed on the actionbar. You must set showasaction="always" alive showasaction="ifroom" in the xml file to display it on the actionbar. Only the menu displayed on the actionbar,There will be icons.To set it in code,menu.finditem (id) .setshowasaction (menuitem.show_as_action_always)

2. Overload onoptionsitemselected (menuitem item) method to capture menu item events

@override
public boolean onoptionsitemselected (menuitem item)
{
 //todo auto-generated method stub
 if (item.getgroupid () == 0&&item.getitemid () == 1)
 {
  intent intent=new intent (this, secondactivity.class);
  startactivity (intent);
 }
 else
 {
  toast.maketext (this, item.gettitle (), toast.length_short) .show ();
 }
 return super.onoptionsitemselected (item);
}

Contextmenu

When the user long presses a control,The pop-up menu is called context menu.We often right-click in windows to bring up the context menu.

1. Overload activity's oncreatecontextmenu () method, call menu's add method to add menu item menuuit

@override
public void oncreatecontextmenu (contextmenu menu, view v,  contextmenuinfo menuinfo)
{
  menu.add (0, 1, 0, "red background");
  menu.add (0, 2, 0, "green background");
  menu.add (1, 3, 0, "White background");
 //todo auto-generated method stub
 super.oncreatecontextmenu (menu, v, menuinfo);
}

2. Overload oncontextitemselected () method, respond to menu click event

@override
public boolean oncontextitemselected (menuitem item)
{
 //todo auto-generated method stub
  switch (item.getitemid ()) {
   case 1:
    mytext.setbackgroundcolor (color.red);
   break;
   case 2:
    mytext.setbackgroundcolor (color.green);
   break;
   case 3:
    mytext.setbackgroundcolor (color.white);
   break;
   }
 return true;
}

3. Overload the registerforcontextmenu () method to register the context menu for the view

private textview mytext;
@override
protected void oncreate (bundle savedinstancestate)
{
 super.oncreate (savedinstancestate);
 setcontentview (r.layout.activity_main);
 mytext=(textview) findviewbyid (r.id.mytext);
 registerforcontextmenu (mytext);
}

Submenu

A submenu is a menu that displays groups of the same function in multiple levels.For example, in the "File" menu of Windows, there are "New", "Open", "Close" and other submenus.

How to create a submenu

1. Overload activity's oncreateoptionsmenu () method, call menu's addsubmenu () method to add submenu items

2. Call submenu's add () rice to add submenu items

@override
public boolean oncreateoptionsmenu (menu menu)
{
 //inflate the menu;this adds items to the action bar if it is present.
 submenu submenu=menu.addsubmenu (0, 4, 4, "login/register");
 submenu.add (1, 1, 1, "login");
 submenu.add (1, 2, 2, "register");
 return true;
}

3. Overload onoptionsitemselected (menuitem item) method to capture menu item events

This method has a menuitem parameter, you can use its gettitle and getitemid methods to determine which menu item was clicked

public boolean onoptionsitemselected (menuitem item) {
 //todo auto-generated method stub
 if (item.gettitle (). equals ("login"))
 {
  log.e ("action:", "Clicked Register/Login");
 }
 return super.onoptionsitemselected (item);
}

submenu is a sub-interface of menu,After adding a submenu, you can add its submenu items through the submenu.add method.Images cannot be displayed on submenu items,But you can display the image in the head of the submenu,However, submenu items can have checkboxes and option buttons.

Note:You cannot add submenus to submenus;

Popup menu

This type of menu needs to be bound to a certain view. When the view is clicked, the menu pops up above or below the view (depending on screen space).

Instructions:

1. Create an XML menu resource file;

popup.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
 <item
  android:id="@ + id/action_edit"
  android:orderincategory="100"
  android:showasaction="never"
  android:title="edit" />
  <item
  android:id="@ + id/action_share"
  android:orderincategory="100"
  android:showasaction="never"
  android:title="popup" />
</menu>

Steps 2 ~ 5 can be implemented in the click event of the bound view!

2. Create a popupmenu object and instantiate the incoming context and bound view;

3. Use getmenuinflater (). Inflate () of the popupmenu object to push the xml menu file in.

4. Use the setonmenuitemclicklistener of the popupmenu object itself to set the click event;

5. Use the popupmenu object's show to display the popup menu.

public void showpopumenu (view v)
{
 popupmenu popup=new popupmenu (mainactivity.this, v);
 menuinflater inflater=popup.getmenuinflater ();
 inflater.inflate (r.menu.popup, popup.getmenu ());
 popup.show ();
}

The above is the menu menu of android development that we introduce to everyone. I hope you like it.

  • Previous JavaScript implements Countdown snippet Item1 (very practical)
  • Next Essential jQuery Practical Skills (Part 2)