Android-context Menu Creation-Your heart

Source: Internet
Author: User

Menu Settings
Covers two methods Oncreateoptionsmenu (Menu menu), onoptionsitemselected (MenuItem Item)
Oncreateoptionsmenu: Create a menu in this function
Onoptionsitemselected: Called when a menu is selected, using switch (Item.getitemid ()) to determine
Parameter 1. Grouping 2.ID 3. Sorting order 4. Title
Menu.add (1, 1, 0, "settings"). SetIcon (r.drawable.setting);
Menu.add (1, 2, 1, "account"). SetIcon (R.drawable.switchuser);
Menu.add (1, 3, 2, "official"). SetIcon (R.drawable.officialweibo);
Menu.add (2, 4, 3, "views"). SetIcon (r.drawable.comment);
Menu.add (2, 5, 4, "about"). SetIcon (R.drawable.aboutweibo);
Menu.add (2, 6, 5, "exit"). SetIcon (R.drawable.menu_exit);
Popup context menu, need Registerforcontextmenu (View v) to register
ListView LV = new Lisrview (this);
Registerforcontextmenu (LV);//Register a ListView context menu
The effect is that you can press and hold an item in the ListView, and a message box appears with a menu option in the box
The following lm.id is the row ID used to display the child view of the context menu. That is, the return value of the Baseadapter method Getitemid (int position) is obtained.
Trigger an item on a context menu
@Override
public boolean oncontextitemselected (MenuItem item) {
TODO auto-generated Method Stub
Adaptercontextmenuinfo lm= (Adaptercontextmenuinfo) item.getmenuinfo ();
Switch (Item.getitemid ()) {
Case 1://Forwarding
Break
Case 2://Comment
Break
Case 3://Collection
Break
Case 4://Viewing information
Break
}
Toast.maketext (this, Item.gettitle (), +). Show ();
return super.oncontextitemselected (item);
}
Create a context Menu
@Override
public void Oncreatecontextmenu (ContextMenu menu, View V,
Contextmenuinfo menuinfo) {
TODO auto-generated Method Stub
Super.oncreatecontextmenu (menu, V, menuinfo);
Adaptercontextmenuinfo lm= (adaptercontextmenuinfo) menuinfo;
System.out.println ("+lm.id+" Item, popup menu registration succeeded ");
Menu.setheadertitle ("+lm.id+" Item's popup menu ");
Menu.add (1, 1, 1, "forward");
Menu.add (1, 2, 2, "comments");
Menu.add (1, 3, 3, "collection");
Menu.add (1, 4, 4, "View information
}

Android-context Menu Creation-Your heart

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.