Android: onCreateOptionsMenu() item action

后端 未结 4 2123
南旧
南旧 2021-02-06 23:49

I have a menu created through:

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(\"Email\");

    return super.onCreateOptionsMenu(menu)         


        
4条回答
  •  别跟我提以往
    2021-02-07 00:42

    That won't work. You should define IDs for your menu items:

    public static final int MENU_ADD = Menu.FIRST;
    public static final int MENU_DELETE = Menu.FIRST + 1;
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
    
        menu.add(Menu.NONE, MENU_ADD, Menu.NONE, "Add");
        menu.add(Menu.NONE, MENU_DELETE, Menu.NONE, "Delete");
        return true;
    }
    
       @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
            switch(item.getItemId())
            {
                case MENU_ADD:
    
                return true;
            case MENU_DELETE:
    
                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }
    

提交回复
热议问题