隐藏弹出菜单项弹出、菜单项

2023-09-07 23:10:41 作者:走在冷风中

我创建了一个popupmenu.I需要从条件弹出菜单隐藏特定的项目,我想这低于code,但它不工作,并显示意外的程序已经被停止。我用findviewbyId和的setEnabled false.is有任何其他的方法来隐藏从弹出菜单中的项目?请帮助这一点。

  @覆盖公共布尔onOptionsItemSelected(菜单项项){        // TODO自动生成方法存根        开关(item.getItemId()){        案例R.id.addMessage_Action:            方法addMessage();            打破;        案例R.id.dropMenuAction:            menuItemView = findViewById(R.id.dropMenuAction);            PopupMenu的弹出=新PopupMenu的(这一点,menuItemView);            MenuInflater吹气= popup.getMenuInflater();            inflater.inflate(R.menu.popupmenu_for_message_delete,popup.getMenu());            popup.show();            popup.setOnMenuItemClickListener(本);            如果(Global.lock ==真)                findViewById(R.id.lock_message).setEnabled(假);            其他                findViewById(R.id.unlock_message).setEnabled(假);            打破;        }        返回false;    } 

解决方案

您需要获得菜单对象从的弹出菜单你的项目了。所以这将会是类似

 菜单弹出菜单= popup.getMenu();如果(Global.lock ==真)    popupMenu.findItem(R.id.lock_message).setEnabled(假);其他    popupMenu.findItem(R.id.unlock_message).setEnabled(假); 
win10家庭版怎么关闭系统自动弹出表情框

和我想这样做,你打电话之前 popup.show()

I created a popupmenu.I need to hide a particular item from popupmenu on condition,i tried this below code but it doesn't work and shows "unexpectedly your app has been stopped".I used findviewbyId and setenabled false.is there any other way to hide an item from popupmenu ?? please help with this.

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()){
        case R.id.addMessage_Action:
            AddMessage();
            break;
        case R.id.dropMenuAction:
            menuItemView = findViewById(R.id.dropMenuAction);
            PopupMenu popup = new PopupMenu(this, menuItemView);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.popupmenu_for_message_delete, popup.getMenu());
            popup.show();
            popup.setOnMenuItemClickListener(this);
            if(Global.lock == true)
                findViewById(R.id.lock_message).setEnabled(false);
            else
                findViewById(R.id.unlock_message).setEnabled(false);

            break;
        }
        return false;

    }

解决方案

You need to get the Menu Object from the PopupMenu before you get the item. So it'd be something like

Menu popupMenu = popup.getMenu();
if(Global.lock == true)
    popupMenu.findItem(R.id.lock_message).setEnabled(false);
else 
    popupMenu.findItem(R.id.unlock_message).setEnabled(false);

And I'd do this before you call popup.show()