Android menu details (4) -- use context menu contextmenu

Source: Internet
Author: User

Previously, the option menu was described in details in "android menu details (II) -- create and respond to option Menus" and "android menu details (iii) -- submenu and iconmenu, sub-menu and Icon menu. Next, let's talk about another widely used menu, context menu.

Contextmenu

In Windows, we are used to right-clicking a file to perform operations such as "open", "RENAME", "cut", and "delete, right-click the context menu. You may smile: "Haha, you won't even use the shortcut keys, will you ?". Cough, this... For example. That's right. The shortcut keys in Windows can help us improve the operation efficiency, but this option does not work in Android. Note: The context menu item of Android isCannot Use shortcut keys. Because the mobile phone operation method is different from the PC operation method using the mouse, Android uses a long-pressed view element to bring up the context menu (PS: currently, most smart phones use full touch screens, there is no physical keyboard, and there is no need to use shortcuts. This innovation is attributed to the revolutionary mobile phone iPhone launched by Steve Jobs in 07. Old Joe retired and pays tribute to him !). In additionIcons and sub-menusCannot be used in context menu items of Android. So how can I use the context menu of Android? See

The context menu inherits Android. View. menu, so we can add a menu item to the context menu like the Options menu operation. The biggest difference between context menus and options menus is that,The options menu owner is activity, and the context menu owner is view in activity.. Each activity has only one Options menu, which serves the whole activity. A single activity usually has multiple views. Not every view has a context menu, which requires usDisplay throughRegisterforcontextmenu (view)To specify.

Although the context menu owner is view, the context menu is generated through oncreatecontextmenu (contextmenu menu, view V, contextmenu in activity. contextmenuinfo menuinfo) method, which is similar to the oncreateoptionsmenu (menu) method that generates the Options menu. The difference between the two is that oncreateoptionsmenu is only used by the userFirst timeWhen you press the "menu" Key, oncreatecontextmenu is calledEach timeLong-pressed view is called, and view must have registered the context menu.

Another important thing to note isContextmenuinfoThe object of this class is passed into the oncreatecontextmenu (contextmenu menu, view V, contextmenu. contextmenuinfo menuinfo) method. What is its usage? Sometimes, the view element needs to pass some information to the context menu, such as the ID of the DB record corresponding to the view, which requires contextmenuinfo. The getcontextmenuinfo () method must be rewritten for the view that needs to pass additional information. A contextmenuinfo implementation Class Object with data is returned.

The following is a demo of how to create and respond to the context menu:

1. register the context menu for a view in the oncreate (...) method of activity

2. Generate the context menu in oncreatecontextmenuinfo.

3. Respond to the context menu item in oncontextitemselected.

Demo: Use context menu

1) Registration context menu
/**
* Context menu demo
*
* @ Author codingmyworld 03:22:39
*/
Publicclass samplecontextmenuactivity extends listactivity {
Privatestaticfinal string tag = "samplecontextmenuactivity ";

@ Override
Protectedvoid oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Display the list
Simpleshowlist ();
// Register the context menu for all list items
This. registerforcontextmenu (getlistview ());
}

Privatevoid simpleshowlist (){
// List item
String [] files = new string [] {
"File 1 ",
"File 2 ",
"File 3 ",
"File 4"
};
// Simple array Adapter
Arrayadapter <string> adapter = new arrayadapter <string> (
This,
Android. R. layout. simple_list_item_1,
Files );
// Set Adapter
This. setlistadapter (adapter );
Log. V (TAG, "Show simple List ");
}
}

 

2) generate context menu

Override method in activity.

@ Override
Publicvoid oncreatecontextmenu (contextmenu menu, view V,
Contextmenuinfo menuinfo ){
Log. V (TAG, "populate context menu ");
// Set context menu title
Menu. setheadertitle ("File Operations ");
// Add context menu item
Menu. Add (0, 1, menu. None, "send ");
Menu. Add (0, 2, menu. None, "marked as important ");
Menu. Add (0, 3, menu. None, "RENAME ");
Menu. Add (0, 4, menu. None, "delete ");
}

 

3) response context menu item

Similar to the response options menu, the only difference is that you can obtain additional information through menu info.

@ Override
Publicboolean oncontextitemselected (menuitem item ){
// Obtain the information of the currently selected item
Adaptercontextmenuinfo menuinfo = (adaptercontextmenuinfo) item. getmenuinfo ();
Log. V (TAG, "context item seleted id =" + menuinfo. ID );

Switch (item. getitemid ()){
Case1:
// Do something
Break;
Case2:
// Do something
Break;
Case3:
// Do something
Break;
Case4:
// Do something
Break;
Default:
Returnsuper. oncontextitemselected (item );
}
Returntrue;
}

Run the program and call the context menu multiple times. Pay attention to the logcat output. oncreatecontextmenu is called every time.

Conclusion

Now, we have introduced various commonly used menus in Android, but so far we have added menu items through hard encoding. Android provides a more convenient way for this purpose, the following section describes how to generate a Menu Using XML.

Related Article

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.