The Android component ContextMenu implements long press events


This example shares the implementation code of ContextMenu long press event for your reference. The specific contents are as follows

 *The ContextMenu menu menu is that long press a component to pop up the ContextMenu in the middle of the screen. Here, it is set to long press the text box to pop up the * ContextMenu menu menu

public class MyContextMenu extends AppCompatActivity {
     *Called when the activity is first created. 
    final static int CONTEXT_ MENU_ 1 = Menu.FIRST;// It can be understood as the minimum value set for ID
    final static int CONTEXT_MENU_2 = Menu.FIRST + 1;
    final static int CONTEXT_MENU_3 = Menu.FIRST + 2;
    TextView textView = null;// Set member variables
    public void onCreate(Bundle savedInstanceState) {
        textView = (TextView)findViewById(;// Get text
        registerForContextMenu(textView);// Registrar press menu event

     *Create menu
    public void onCreateContextMenu(ContextMenu menu,
                                    View v, ContextMenu.ContextMenuInfo menuInfo){
        Menu.setheadertitle ("shortcut menu title")// Set title
        //Set corresponding ID and title
        Menu.add (0, context_menu_1, 0, "menu sub item 1");
        Menu.add (0, context_menu_2, 1, "menu sub item 2");
        Menu.add (0, context_menu_3, 2, "menu sub item 3");

     *Set corresponding event
    public boolean onContextItemSelected(MenuItem item){
            case CONTEXT_MENU_1:
                Toast. Maketext (mycontextmenu. This, "select menu sub item 1", toast. Length_short). Show();
                return true;
            case CONTEXT_MENU_2:
                Toast. Maketext (mycontextmenu. This, "select menu sub item 2", toast. Length_short). Show();
                return true;
            case CONTEXT_MENU_3:
                Toast. Maketext (mycontextmenu. This, "select menu sub item 3", toast. Length_short). Show();
                return true;
        return false;

    public void skip(View v){
        Intent intent = new Intent();
        intent.setClass(MyContextMenu.this,TabDemo.class);// The former jumps back
        startActivity(intent);// start-up


<LinearLayout xmlns:android=""
    <TextView android:id="@+id/label"


design sketch:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.