2016-04-04 71 views
1

在我的應用我有近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); 
} 

所以這裏在選項菜單我想顯示只有兩個用戶和8管理員..

但是,當我選擇它作爲用戶我和能夠看到所有的菜單和只有他們兩個人正在..所以在這裏我只想顯示工作菜單remaning應隱藏..

任何一個可以建議我這樣的..

這裏的菜單是從Android的菜單不僅沒有動態菜單...

回答

2

在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; 
} 
+0

感謝@Ranjhid庫馬爾先生,我在這裏的時候我用這個在我的代碼我收到錯誤的項目和「菜單」。請檢查我的更新)的問題,我應該需要更改我的代碼 –

+0

@ Don'tBenegative嘗試內onCreateOptionMenu(代碼..看到我的編輯答案.. –

+0

先生,請看到我的更新問題,先生......如果我跟着你我的代碼我正在使用map.get(「usage」)。equals(「User」)from shared prefs .. at resume –

0
private Menu menu1; 

現在的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。做相同的其他選項

相關問題