隐藏弹出菜单项 [英] Hide item from popupmenu

查看:321
本文介绍了隐藏弹出菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个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(假);

和我想这样做,你打电话之前 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()

这篇关于隐藏弹出菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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