ContextMenu Introduction:
Let's say a view booklet has a context menu. When you press and hold the view, a floating menu pops up to select the next action.
Implementing this function requires calling Setoncreatecontextmenulistener to register a listener. So what's the difference between it and the Register long press listener Setonlongclicklistener? The following will be tested in the code, by adding print discovery, when the user presses a view long. The first trigger is the Onlongclick function, which runs the onlongclick operation and then infers whether the Oncreatecontextmenulistener function is running based on the Onlongclick return value. Suppose Onlongclick returns false. The Oncreatecontextmenulistener function continues to run, assuming that it returns true. Ignores the Oncreatecontextmenulistener function.
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>
Suppose you need to close the ContextMenu box yourself. Ability to invoke:
Getactivity (). Closecontextmenu ();
Use of Android ContextMenu