The Android component ContextMenu implements long press events

Time:2021-11-25

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
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_context_menu);
        textView = (TextView)findViewById(R.id.label);// Get text
        registerForContextMenu(textView);// Registrar press menu event
    }

    /**
     *Create menu
     **/
    @Override
    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
     **/
    @Override
    public boolean onContextItemSelected(MenuItem item){
        switch(item.getItemId()){
            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
    }
}

xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MyContextMenu"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        />

</LinearLayout>

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.