Context menu example

Table of contents

Create an Android application ContextMenuDemo based on the Empty Activity template

Copy the image material to the drawable directory

 Open the string resource file strings.xml and enter the code

Open the main layout resource file acitivity_main.xml and enter the code:

 Open the main interface class MainActivity and enter the code:


Empty ActivityCreate an Android application based on a templateContextMenuDemo

 

Copy the image material to drawablethe directory

 Open string resource filestrings.xml输入代码

Specific code:

<resources>
    <string name="app_name">Context menu demo</string>
    <string name="file">文件</string>
    <string name="edit">编辑</string>
</resources>

Open the main layout resource fileacitivity_main.xml输入代码:

 Specific code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:background="#eeeeee">

        <TextView
            android:id="@+id/tv_file"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:text="@string/file"
            android:textColor="#0000ff"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tv_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/edit"
            android:textColor="#0000ff"
            android:textSize="20sp" />
    </LinearLayout>
</LinearLayout>

 Open the main interface classMainActivity输入代码:

 

 Specific code:

package net.zyt.contextmenudemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {
    private TextView tvFile;//File label
    private TextView tvEdit;//Edit label

    private static final int NEW_FILE_MENU_ITEM=1;//new file menu item identification
    private static final int OPEN_FILE_MENU_ITEM=2;//open file menu item identification
    private static final int SAVE_FILE_MENU_ITEM=3;//Save file menu item ID
    private static final int EXIT_FILE_MENU_ITEM=4;//Exit file menu item identification

    private static final int CUT_MENU_ITEM=5;//Cut menu item identification
    private static final int COPY_MENU_ITEM=6;//copy menu item ID
    private static final int PASTE_MENU_ITEM=7;//Paste the menu item ID

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Use the layout resource file to set up the user interface
        setContentView(R.layout.activity_main);
        / / Get the control instance through the resource identifier
        tvFile=findViewById(R.id.tv_file);
        tvEdit=findViewById(R.id.tv_edit);
        // Register the context menu for the two label controls
        registerForContextMenu(tvFile);
        registerForContextMenu(tvEdit);

    }
    /**
     * Methods available for setting icons
     *
     * @param menu
     * @param enabled
     */
    private void setIconEnabled(Menu menu, boolean enabled) {
        try {
            Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
            Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
            m.setAccessible(true);
            // MenuBuilder implements the Menu interface. When creating a menu, the menu passed in is actually a MenuBuilder object (polymorphic feature of Java)
            m.invoke(menu, enabled);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu,v,menuInfo);
        //Set menu icon available
        setIconEnabled(menu,true);
        //Create different context menus for different controls
        switch (v.getId()){
            case R.id.tv_file://file tag
                //Set the title icon
                menu.setHeaderIcon(R.drawable.file);
                //set menu title
                menu.setHeaderTitle(R.string.file);
                //Add menu item (group ID, menu item ID, menu item serial number, menu item title)
                menu.add(1,NEW_FILE_MENU_ITEM,1,"New file").setIcon(R.drawable.new_file);
                menu.add(1,OPEN_FILE_MENU_ITEM,2,"Open File").setIcon(R.drawable.open_file);
                menu.add(1,SAVE_FILE_MENU_ITEM,3,"Save file").setIcon(R.drawable.save_file);
                menu.add(1,EXIT_FILE_MENU_ITEM,4,"Exit file").setIcon(R.drawable.exit_app);
                break;
            case R.id.tv_edit://edit tag
                //Set the menu icon
                menu.setHeaderIcon(R.drawable.edit);
                //set menu title
                menu.setHeaderTitle(R.string.edit);
                //Add menu item (group ID, menu item ID, menu item serial number, menu item title)
                menu.add(2,CUT_MENU_ITEM,1,"剪切").setIcon(R.drawable.cut);
                menu.add(2,COPY_MENU_ITEM,2,"复制").setIcon(R.drawable.copy);
                menu.add(2,PASTE_MENU_ITEM,3,"粘贴").setIcon(R.drawable.paste);
                break;
        }
}
//Context menu item selection event processing method
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        //Determine which menu item is selected with the lake
        switch (item.getItemId()){
            case NEW_FILE_MENU_ITEM://new file menu item
                Toast.makeText(this,"You clicked the [New File] menu item!",Toast.LENGTH_SHORT).show();
                break;
            case OPEN_FILE_MENU_ITEM://open file menu item
                Toast.makeText(this,"You clicked the 【Open File】menu item!", Toast.LENGTH_SHORT).show();
                break;
            case SAVE_FILE_MENU_ITEM://save file menu item
                Toast.makeText(this,"You clicked the [Save File] menu item!",Toast.LENGTH_SHORT).show();
                break;
            case EXIT_FILE_MENU_ITEM://exit file menu item
                finish();//Close the current window and exit the application
                break;
            case CUT_MENU_ITEM://Cut menu item
                Toast.makeText(this,"You clicked the [Cut] menu item!",Toast.LENGTH_SHORT).show();
                break;
            case COPY_MENU_ITEM://copy menu item
                Toast.makeText(this,"You clicked the [Copy] menu item!",Toast.LENGTH_SHORT).show();
                break;
            case PASTE_MENU_ITEM://paste menu item
                Toast.makeText(this,"You clicked the [Paste] menu item!",Toast.LENGTH_SHORT).show();
        }
        return true;
    }
}

Final effect:

Guess you like

Origin blog.csdn.net/hollow_future/article/details/128189119