android菜单代码不起作用 [英] android menu code not working

查看:26
本文介绍了android菜单代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图弄清楚为什么当我按下按钮时我的布尔值没有改变,当我手动改变它时它工作,但它没有做任何事情.我试图按照这个词的教程进行操作,但它们不起作用.有人能指出我哪里出错了吗?

I have been trying to figure out why my boolean is not changing when I press the button, when I changed it manually it worked, but it doesn't do any thing. I have tried to follow tutorials to the word but they don't work. Can anybody point out where I am going wrong?

public boolean onOptionsItemSelected(MenuItem menu) 
{
    MenuItem freeze = (MenuItem)findViewById(R.id.freeze);  
    // Handle item selection 
    switch (menu.getItemId()) { 
        case R.id.freeze: 
            if (freze == false){
                freze = true;
            } else {
                freze = false;
            }
            return true; 
        case R.id.toggleVolCount: 
            if (toggleVol == true){
                toggleVol = false;
            } else {
                toggleVol = true;
            }
            return true; 
        default: return super.onOptionsItemSelected(menu); 
    } 

感谢您的所有帮助,当我尝试了建议的代码但它不起作用时,我返回并更改了菜单.以前我制作了一个带有 onClick 的按钮来创建菜单,当使用代码创建图标时,我之前编写的代码工作正常.希望这对我以外的人有所帮助,这样我就不会觉得自己像个白痴.}

Thanks for all your help, when I tried the code that was suggested and it didn't work I went back and changed the menu. Previously I had made a button with an onClick to create the menu, when created the icon with code the code that I had previously written worked fine. Hope this helps someone other than me so I don't feel like so much of an idiot.}

推荐答案

在 res 文件夹中创建一个类似 drawable 的文件夹菜单

In res folder create one folder menu like drawable

在该文件夹中创建新的 xml 文件 optionmenu.xml.

Create new xml file optionmenu.xml in that folder.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menuitem" 
        android:title="Prefs">
    </item>
         <item android:id="@+id/menuitem1" 
        android:title="Prefs1">
    </item>


</menu>

在 onCreate 方法中编写此代码....

In onCreate method write this code....

setOptionMenu(R.menu.optionmenu);

并在 Menu 的覆盖方法中编写此代码.....

and in overide method of Menu write this code.....

@Override
    public boolean onOptionsItemSelected(MenuItem menu) {
        switch (menu.getItemId()) {
        case R.id.menuitem:
            startActivity(new Intent(this, Prefs.class));
            break;

case R.id.menuitem1:
            startActivity(new Intent(this, Prefs1.class));
            break;
        default:
            break;
        }

        return true;
    }

这篇关于android菜单代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆