Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在actionbar中隐藏一些菜单?_Android_Android Optionsmenu - Fatal编程技术网

Android 如何在actionbar中隐藏一些菜单?

Android 如何在actionbar中隐藏一些菜单?,android,android-optionsmenu,Android,Android Optionsmenu,在我的应用程序中,我有将近8个菜单,我有两种类型的用户:管理员和客户端 对于管理员,我需要显示所有8个手册,对于用户,我需要为用户显示2个菜单 为此,我给了这个 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.m_mymenu, menu); return true; } 在我的代码出现在简历上之后,我在下面添加了一个 @Ove

在我的应用程序中,我有将近8个菜单,我有两种类型的用户:管理员和客户端

对于管理员,我需要显示所有8个手册,对于用户,我需要为用户显示2个菜单

为此,我给了这个

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.m_mymenu, menu);
    return true;      
}
在我的代码出现在简历上之后,我在下面添加了一个

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    SharedPreferences s = getSharedPreferences(my_New_SP, 0);
    HashMap<String, String> map= (HashMap<String, String>) s.getAll();

    int id = item.getItemId();

 if (map.get("usage").equals("Admin"))
 {
    if (id == R.id.abc) {
        Intent mypIntent = new Intent(this, Myp.class);
        startActivity(mypIntent);
        return true;
    }
    .
    .
    .
    .
    else if (id == R.id.web1) {
        Intent webIntent = new Intent(this, Web.class);
        startActivity(webIntent);
        return true;
    }
    else if (id == R.id.about1) {
        Intent aboutIntent = new Intent(this, About.class);
        startActivity(aboutIntent);
        return true;
    }   
}

if (map.get("usage").equals("User"))
{
    if (id == R.id.web1) {
        Intent webIntent = new Intent(this, Web.class);
        startActivity(webIntent);
        return true;
    }
    else if (id == R.id.about1) {
        Intent aboutIntent = new Intent(this, About.class);
        startActivity(aboutIntent);
        return true;
    }
}   
    return super.onOptionsItemSelected(item);
}
@覆盖
公共布尔值onOptionsItemSelected(菜单项项){
SharedReferences s s=GetSharedReferences(my_New_SP,0);
HashMap=(HashMap)s.getAll();
int id=item.getItemId();
if(map.get(“用法”).equals(“管理”))
{
如果(id==R.id.abc){
Intent mypIntent=新Intent(this,Myp.class);
星触觉(mypIntent);
返回true;
}
.
.
.
.
else if(id==R.id.web1){
Intent webIntent=新的Intent(这个,Web.class);
startActivity(webIntent);
返回true;
}
else if(id==R.id.about1){
Intent aboutint=新Intent(this,About.class);
星体触觉(大约);
返回true;
}   
}
if(map.get(“用法”).equals(“用户”))
{
if(id==R.id.web1){
Intent webIntent=新的Intent(这个,Web.class);
startActivity(webIntent);
返回true;
}
else if(id==R.id.about1){
Intent aboutint=新Intent(this,About.class);
星体触觉(大约);
返回true;
}
}   
返回super.onOptionsItemSelected(项目);
}
所以在选项菜单中,我只想为用户显示两个选项,为管理员显示8个选项

但是当我选择它作为用户时,我可以看到所有的菜单,只有两个菜单在工作。。所以这里我只想显示remaning应该隐藏的工作菜单

有谁能向我推荐这种类型的吗

这里的菜单仅来自android菜单,而不是动态菜单…

在OnCreateOptions菜单()中,检查条件并按以下方式显示或隐藏:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.layout.m_mymenu, menu);


SharedPreferences s = getSharedPreferences(my_New_SP, 0);
HashMap<String, String> map= (HashMap<String, String>) s.getAll();

if (map.get("usage").equals("Admin"))
 {
 MenuItem item = menu.findItem(R.id.usermenu1);
 item.setVisible(false); 
 //your all user menu's same like above

}


else if (map.get("usage").equals("User"))
{
 MenuItem item = menu.findItem(R.id.adminmenu1);
 item.setVisible(false); 
 //your all admin menu's same like above
}

  return true;
}
@覆盖
公共布尔onCreateOptions菜单(菜单){
MenuInflater MenuInflater=getMenuInflater();
菜单充气(R.layout.m_mymenu,menu);
SharedReferences s s=GetSharedReferences(my_New_SP,0);
HashMap=(HashMap)s.getAll();
if(map.get(“用法”).equals(“管理”))
{
MenuItem item=menu.findItem(R.id.usermenu1);
项。设置可见(假);
//您的所有用户菜单与上述相同
}
else if(map.get(“用法”).equals(“用户”))
{
MenuItem=menu.findItem(R.id.adminmenu1);
项。设置可见(假);
//你的所有管理菜单都和上面一样
}
返回true;
}
现在在oncreate中执行类似的操作

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        menu1 = menu.findItem(R.id.web1);
        menu1.setVisible(false);
        return true;
    }

现在,您可以随时隐藏/显示
menu1
。对其他选项也一样

谢谢@Ranjhid Kumar先生,当我在代码中使用此选项时,我在项目和“菜单”中遇到错误。请检查我更新的问题,我应该在哪里更改我的密码code@Don'tBenegative请尝试onCreateOptionMenu()中的代码。。请看我编辑的答案。先生,请看我的更新问题,先生。。。如果我遵循你的代码,我会得到错误。。。这里我使用的是来自共享prefs的map.get(“用法”).equals(“用户”)。。马上resume@Don‘t告诉对方错误的路线&’错误详细信息..检查我的更新答案是的,先生,我在使用共享优先权,在简历上是指在菜单中是我问题的一部分。。。在我的代码中,菜单是分开的,菜单项是分开的。。
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        menu1 = menu.findItem(R.id.web1);
        menu1.setVisible(false);
        return true;
    }