Create a pop-up menu for Android

Source: Internet
Author: User




Reprinted: http://houwanji.blog.163.com/blog/static/1622762200972644732128/
First, add the registerforcontextmenu (getlistview (); statement at the end of the oncreate function,

This means that every item of the listview in the screen is regardedMenu
To register,

Then inherit the oncreatecontextmenu () method as follows:

@ Override //Create
Long pressPop-up
Menu
This is a callback function.
Public void oncreatecontextmenu (contextmenu menu, view V, contextmenuinfo menuinfo ){
Super. oncreatecontextmenu (menu, V, menuinfo );
Menu. Add (0, delete_id, 0, R. String. menu_delete); // Add twoMenu
Item
Menu. Add (0, insert_id, 0, R. String. menu_insert );
}

Finally, SetMenu
Action after the item is selected

@ Override
Public Boolean oncontextitemselected (menuitem item ){
Switch (item. getitemid ()){
Case delete_id:
Adaptercontextmenuinfo info = (adaptercontextmenuinfo) item. getmenuinfo (); // getMenu
Information
Mdbhelper. deletenote (info. ID); // Delete selected note
Filldata ();
Return true;
}
Return super. oncontextitemselected (item );

}

You can use the setonitemlongclick function of listview to associate a long press event. However, in the onitemlongclick function of the response, you cannot call the listview display function.Menu
.
InAndroid
OfMenu
There are two types: Options menu and context menu. (There are also three types: Options Menu
, Context menu, sub menu) where options menu is associated with activity, context menu
Is associated with the view. We need to clickMenu
Belongs to the context menu, but the context menu
Only setoncreatecontextmenulistener can be used for association.
To achieve this effect, you need to understand what happens after the listview item is long pressed:

The onitemlongclick function is triggered first, and the onitemlongclick operation is executed. Then, the onitemlongclick function is returned.
Whether the oncreatecontextmenulistener function is executed. If onitemlongclick returns false, the system continues to execute
Oncreatecontextmenulistener function. Otherwise, true is returned. The oncreatecontextmenulistener function is ignored.
Therefore, we can determine the onitemlongclick and prepare the data needed in oncreatecontextmenulistener, and then call the data display in oncreatecontextmenulistener.

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.