当前位置: 首页 > 工具软件 > jump_menu > 使用案例 >

Menu菜单

段干祺
2023-12-01

系统菜单OptionsMenu

  //这是onCreateOptionsMenu菜单和监听
    //重写onCreateOptionsMenu加载资源文件
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //获取连接菜单的布局
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    //重写onCreateOptionsMenu的监听
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case R.id.red:
                Toast.makeText(this, "红的", Toast.LENGTH_SHORT).show();
                break;
            case R.id.blue:
                Toast.makeText(this, "蓝的", Toast.LENGTH_SHORT).show();
                break;
            case R.id.black:
                Toast.makeText(this, "黑的", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

上下文菜单ContextMenu

    //这是上下文菜单ContextMenu
    //重写ContextMenu加载资源文件
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.menu, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    //重写ContextMenu菜单的监听
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case R.id.red:
                Toast.makeText(this, "这是红色", Toast.LENGTH_SHORT).show();
                break;
            case R.id.blue:
                Toast.makeText(this, "这是蓝色", Toast.LENGTH_SHORT).show();
                break;
            case R.id.black:
                Toast.makeText(this, "这是黑色", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

这是弹出菜单

       jumpId = (TextView) findViewById(R.id.jump_id);
        //这是弹出菜单
        jumpId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建弹出菜单对象
                PopupMenu popupMenu = new PopupMenu(MainActivity.this, jumpId);
                //连接加载布局
                popupMenu.inflate(R.menu.jump_menu);
                //设置监听事件
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        int itemId = item.getItemId();
                        switch (itemId) {
                            case R.id.one:
                                Toast.makeText(MainActivity.this, "点击了第一个", Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.two:
                                Toast.makeText(MainActivity.this, "点击了第二个", Toast.LENGTH_SHORT).show();
                            case R.id.three:
                                Toast.makeText(MainActivity.this, "点击了第三个", Toast.LENGTH_SHORT).show();
                                break;
                        }
                        return false;
                    }
                });
                //显示弹出菜单
                popupMenu.show();
            }
        });
 类似资料: