android menu code not working

后端 未结 1 936
无人共我
无人共我 2020-12-22 13:58

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

相关标签:
1条回答
  • 2020-12-22 14:02

    In res folder create one folder menu like drawable

    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>
    

    In onCreate method write this code....

    setOptionMenu(R.menu.optionmenu);
    

    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;
        }
    
    0 讨论(0)
提交回复
热议问题