Home>

The context menu is different from the options menu,The option menu serves the activity, and the context menu is registered to a view object.

If a view object is registered with the context menu,The user can call the context menu by long pressing the view object.

The context menu does not support shortcut keys.The menu options cannot be accompanied by icons.However, you can specify an icon for the title of the context menu.

Following mock context menu

main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout android:id="@ + id/linearlayout01"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  xmlns:android="http://schemas.android.com/apk/res/android">
  <edittext android:text="first text box"
    android:id="@ + id/edittext01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
  <edittext android:text="second text box"
    android:id="@ + id/edittext02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</linearlayout>

meunactivity class

package com.ljq.activity;
import android.app.activity;
import android.os.bundle;
import android.view.contextmenu;
import android.view.menuitem;
import android.view.view;
import android.view.contextmenu.contextmenuinfo;
import android.widget.edittext;
public class meunactivity extends activity {
  private edittext edittext01=null;
  private edittext edittext02=null;
  private final int menu1=1;
  private final int menu2=2;
  private final int menu3=3;
  private final int menu4=4;
  private final int menu5=5;
  @override
  protected void oncreate (bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    setcontentview (r.layout.main);
    edittext01=(edittext) findviewbyid (r.id.edittext01);
    edittext02=(edittext) findviewbyid (r.id.edittext02);
    //Register the context menu for the view object
    this.registerforcontextmenu (edittext01);
    this.registerforcontextmenu (edittext02);
  }
  /**
   * Initialize the context menu
   *
   * Called each time the context menu is called up
   * /
  @override
  public void oncreatecontextmenu (contextmenu menu, view v,      contextmenuinfo menuinfo) {
    menu.setheadericon (r.drawable.header);
    switch (v.getid ()) {
    case r.id.edittext01:
      menu.add (0, menu1, 0, "menu item 1");
      menu.add (0, menu2, 0, "menu item 2");
      menu.add (0, menu3, 0, "menu item 3");
      break;
    case r.id.edittext02:
      menu.add (0, menu4, 0, "menu item 4");
      menu.add (0, menu5, 0, "menu item 5");
      break;
    }
    //super.oncreatecontextmenu(menu, v, menuinfo);
  }
  /**
   * This event is called when the user selects the context menu option
   * /
  @override
  public boolean oncontextitemselected (menuitem item) {
    switch (item.getitemid ()) {
    case menu1:
    case menu2:
    case menu3:
      edittext01.append ("\ n" + item.gettitle () + "pressed");
      break;
    case menu4:
    case menu5:
      edittext02.append ("\ n" + item.gettitle () + "pressed");
      break;
    }
    return true;
  }
}

operation result:

  • Previous Email regular expressions collation
  • Next Batch processing perfectly realizes FTP remote backup data