Android Advanced (28) context menu ContextMenu use case

Source: Internet
Author: User

context Menu ContextMenu use casePreface

Review the previous application before discovering that the options menu created previously could not be displayed. According to normal logic, the left image in the "Product Information" column should have a menu of options, users can share and so on, but now this operation inexplicably disappeared. Wrote a test demo, as shown in the figure, everything according to logical display normal. The blame is not displayed in the project, initially conceived because the Android system version is too high, but after testing on another phone, the problem still exists. Is it because the top tab title bar obscures the display of the Options menu? Continue the test by testing the Options menu in another page that does not have a tab title bar, as shown in the image on the right. So the problem becomes more difficult. It is because of the overall setup of the project, the reasons for this is very large, but because the root cause of the problem is not clear, so there are some difficulties in the revision.

changing ideas

Now that you are unable to complete the operation through the Options menu, read the book to learn that there are two other forms of the Menu: submenu (submenu), shortcut menus (context menu), and try to create a new context menu to solve the problem.

A shortcut menu is similar to a "Right-click menu" In a computer program that launches a shortcut menu that is registered to the interface element when the user clicks on an element in the interface for more than 2 seconds. The shortcut menu also uses a floating display, although the shortcut menu is displayed in the same way as the submenu, but the two menus start differently.

Shortcut menus are used in much the same way as options menus, except that overloaded functions are different. The shortcut menu needs to overload the Oncreatcontextmenu () function to initialize the menu items, including the caption, icon, and menu subkey that the shortcut menu displays.

Note

For the option menu creation code, see the blog post "Android Advanced (26) Menuinflater Implementation Menu Add", no longer show here.

Source Code

Add a shortcut menu//1. Override Oncreatecontextmenu (Menu menu) method in which the menu object's method is called to add an item or submenu @overridepublic void Oncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {menuinflater inflater = new Menuinflater (this) ///Reload R.menu.production_info_activity_menu corresponding menu and add Inflater.inflate (R.menu.production_info_activity_menu, menu); Menu.setheadertitle ("Quickmark"); Super.oncreatecontextmenu (menu, V, menuinfo);} 2. If you want the application to be able to click events for the corresponding menu item, rewrite the activity's oncontextitemselected (MenuItem Item) method to @overridepublic a Boolean Oncontextitemselected (MenuItem Item) {//Gets the Id,int item_id = Item.getitemid () of the currently selected MenuItem,//determines which menu item is clicked, and targeted to respond to switch (item_id) {case R.id.pia_menuitem_fenxiang: {Intent Intent = new Intent (intent.action_send);//Set Sharing type/ /intent.settype ("image/*"); Intent.settype ("Text/plain"); Intent.putextra (Intent.extra_subject, "Share");//the show is here! Intent.putextra (Intent.extra_text, "#我正在使用" quickmark "Shopping evaluation sharing software (share from Huaqiang app) #" + product.tostring ()); Intent.setflags ( Intent.flag_activity_new_task); startactIvity (Intent.createchooser (Intent, GetTitle ());}; Break;case R.id.pia_menuitem_shuaxin:{}}return true;}

Register the shortcut menu to a control code in the interface as follows:

     public void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.production_info_activity);         Register the shortcut menu on the product QR code image        LinearLayout layout = (linearlayout) Findviewbyid (r.id.pia_linearlayout);        Registerforcontextmenu (layout);        .....}

Among them, R.id. Pia_linearlayout is the ID of the overall linear layout.

The effect is as follows:

The shortcut menu is now created.

the difference between Oncreatecontextmenu and Oncreateoptionsmenu

The Oncreateoptionsmenu function is only called once when the menu is first used;

The Oncreatecontextmenu function will be called once each time it is started.

Reference Documents

1. The second edition of Android app development

2.http://www.2cto.com/kf/201309/247051.html

American and American pictures


Android Advanced (28) context menu ContextMenu use case

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.