Android的菜单code不工作菜单、工作、Android、code

2023-09-13 02:22:32 作者:扑你怀里

我一直在试图找出为什么我的布尔是不会改变的时候我preSS的按钮,当我手动改变了它,它的工作,但它不会做任何事情。我曾试图按照教程,单词,但他们不工作。任何人都可以指出我要去的地方错了?

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); 
    } 

感谢你的帮助,当我试图在code的建议,并没有工作,我回去,并改变了菜单。 previously我犯了一个按钮,一个onClick创建菜单,当创建了$ C C的code,我有previously书面$图标工作得很好。希望这可以帮助别人比我其他的,所以我不觉得像这么大白痴。}

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.}

推荐答案

在资源文件夹中创建一个像绘制一个文件夹菜单

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方法写code ....

In onCreate method write this code....

setOptionMenu(R.menu.optionmenu);

和在菜单的超越控制的方法写这篇code .....

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;
    }