如何通过Click Event以编程方式显示/隐藏操作栏项目 [英] How to Show/Hide Action Bar item programmatically via Click Event

查看:79
本文介绍了如何通过Click Event以编程方式显示/隐藏操作栏项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,我使用以下代码将可见性设置为false.

By default I set the visibility to false by using following code.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_items, menu);
       menu.findItem(R.id.action_share).setVisible(false);
        return true;
    }

现在,当用户单击我的活动中的按钮时,如何再次使其可见.

Now how can I make it visible again when user clicks a button in my activity.

推荐答案

在您的onCreateOptionsMenu中:

In your onCreateOptionsMenu:

public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_items, menu);
       if (hideIcon){
          menu.findItem(R.id.action_share).setVisible(false);
       }else{
          menu.findItem(R.id.action_share).setVisible(true);
       }
        return true;
    }

在要显示/隐藏图标的方法中,只需将boolean hideIcon设置为true或false并调用:

In method where you want to show/hide the icon, just set the boolean hideIcon to true or false and call :

invalidateOptionsMenu();

刷新菜单.

这篇关于如何通过Click Event以编程方式显示/隐藏操作栏项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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