Android基础——选项菜单,上下文菜单

时间:2022-02-22 17:58:06

首先要在资源目录下新建menu目录,所有的菜单布局文件都在这个目录下

选项菜单和上下文菜单的资源文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@ id/message" android:title="消息"></item>
    <item android:id="@ id/homepage" android:title="首页"></item>
    <item android:id="@ id/help" android:title="帮追"></item>
    <item android:id="@ id/feedback" android:title="我要反馈"></item>
    <item android:id="@ id/setting" android:title="设置"></item>
    <item android:id="@ id/regard" android:title="关于"></item>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@ id/copy" android:title="复制"></item>
    <item android:id="@ id/save" android:title="保存"></item>
</menu>

java代码

package com.example.mymenui;

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

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


public class MainActivity extends AppCompatActivity {
    private TextView textView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
         * 2:上下文菜单
         * */
        //2.1:配置上下文菜单xml文件

        //2.2:为文本框注册上下文菜单
        textView = (TextView)findViewById(R.id.text1);
        registerForContextMenu(textView);
    }
    //2.3:重写onCreateContextMenu()方法,添加一个上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater menuInflater = new MenuInflater(this);
        menuInflater.inflate(R.menu.contextmenu,menu);
    }
    //2.4:指定各个菜单项被选择时做出的处理
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.copy:
                Toast.makeText(
                        MainActivity.this,"已复制",Toast.LENGTH_SHORT
                ).show();
                break;
            case R.id.save:
                Toast.makeText(
                        MainActivity.this,"已保存",Toast.LENGTH_SHORT
                ).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

    /*
     * 1:使用选项菜单的
     *   1.1:配置菜单的xml文件
     * */
    //1.2:重写onCreateOptionsMenu() 方法,添加一个选项菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //实例化MenuInflater对象
        MenuInflater menuInflater = new MenuInflater(this);
        //解析菜单资源文件
        menuInflater.inflate(R.menu.menu,menu);
        //返回这个菜单
        return super.onCreateOptionsMenu(menu);
    }
    //1.3:onOptionsItemSelected()对各个菜单项进行处理
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.setting:
                Intent intent1 = new Intent(
                        MainActivity.this,Setting.class
                );
                startActivity(intent1);
                break;
            case R.id.regard:
                Intent intent2 = new Intent(
                        MainActivity.this,Regard.class
                );
                startActivity(intent2);
                break;
    }
        return super.onOptionsItemSelected(item);
    }


}