gpt4 book ai didi

android - 如何隐藏操作栏中的一些菜单?

转载 作者:行者123 更新时间:2023-11-29 20:00:02 24 4
gpt4 key购买 nike

在我的应用程序中,我有近 8 个菜单,我有两种类型的用户 admin 和 client

对于管理员,我需要显示所有 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);
}

所以在选项菜单中,我只想为用户显示两个,为管理员显示 8 个。

但是当我选择它作为用户 I 并且能够看到所有菜单时,只有其中两个在工作..所以在这里我只想显示工作菜单 remaning 应该隐藏..

谁能给我这样的建议..

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

最佳答案

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

@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;
}

关于android - 如何隐藏操作栏中的一些菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36397968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com