Use of Android ContextMenu

Source: Internet
Author: User

ContextMenu Introduction:

If a view registers a context menu, a floating menu pops up when you press and hold the view to select the next action.

Implementing this function requires calling Setoncreatecontextmenulistener to register a listener, so what is the difference between registering a long press listener Setonlongclicklistener? The following will be tested in the code, by adding print discovery, the user to a view long press, the first trigger is the Onlongclick function, perform the onlongclick operation, The Oncreatecontextmenulistener function is then judged based on the return value of the Onlongclick, if Onlongclick returns False, The Oncreatecontextmenulistener function continues to execute, and if true, the Oncreatecontextmenulistener function is ignored.

The code is as follows:

public class Mainactivity extends actionbaractivity {ImageView mimageview; Mycontextmenuclicklistener mcontextlistener; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.fragment_main); Mimageview = (ImageView) findViewById ( R.id.image); mcontextlistener = new Mycontextmenuclicklistener (); Mimageview.setoncreatecontextmenulistener ( Mcontextlistener);//testmimageview.setonlongclicklistener (new Onlongclicklistener () {@Overridepublic Boolean Onlongclick (View arg0) {//TODO auto-generated method Stublog.d ("Test", "Onlongclick"); return false;});} Private class Mycontextmenuclicklistener implements Oncreatecontextmenulistener, onmenuitemclicklistener{@ Overridepublic boolean Onmenuitemclick (MenuItem Item) {//TODO auto-generated method Stubswitch (Item.getitemid ()) {case R.ID.OPEN:LOG.D ("Test", "click Open"); Break;case r.id.delete:log.d ("Test", "click Delete"); Break;case R.id.remove: LOG.D ("Test", "click Remove"); Break;default:breaK;} return true;} @Overridepublic void Oncreatecontextmenu (ContextMenu menu, View v,contextmenuinfo menuinfo) {//TODO auto-generated Method Stublog.d ("Test", "Oncreatecontextmenu"); Menuinflater inflater = Getmenuinflater (); Inflater.inflate (R.menu.context_menu, menu); Layoutinflater layoutinflater = Getlayoutinflater (); View view = (view) layoutinflater.inflate (R.layout.menu_header, null); Menu.setheaderview (view); MenuItem open = Menu.finditem (R.id.open); Open.setonmenuitemclicklistener (Mcontextlistener); MenuItem Delete = Menu.finditem (r.id.delete);d Elete.setonmenuitemclicklistener (Mcontextlistener); MenuItem remove = Menu.finditem (R.id.remove); Remove.setonmenuitemclicklistener (Mcontextlistener);}}}

Context_menu.xml

<?xml version= "1.0" encoding= "UTF-8"?> <menu    xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <group android:id=" @+id/context_menu ">    <item android:id=" @+id/delete "        android: title= "Delete"/>        <item android:id= "@+id/open"        android:title= "open"/> <item           android:id= "@ +id/remove "        android:title=" Remove "/>       </group>    </menu>

Menu_header.xml

<?xml version= "1.0" encoding= "UTF-8"? ><linearlayout    xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_height=" wrap_content "    android:layout_width=" wrap_content "    >    <textview         android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:text = "This is a Test"        />    </LinearLayout>


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.