2016-05-31 12 views
4

デフォルトでは、次のコードを使用して可視性をfalseに設定します。ユーザーが私の活動のボタンをクリックしたときにクリックイベントを使用してアクションバーアイテムをプログラムで表示/非表示する方法

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

は今どのように私は再びそれを見えるようにすることができます。あなたのonCreateOptionsMenuで

答えて

5

:あなたはアイコンを表示/非表示にする方法では

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

、単にtrueまたはfalseにブールhideIconを設定して呼び出します。

invalidateOptionsMenu(); 

をメニューをリフレッシュします。

+0

ありがとうございます。 – Ramesh

1

そのメニュー項目のインスタンスを取得し、その項目の可視性を毎回設定できます。

 Menu mMenu; 
     @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); 
      mMenu = menu; 
      mMenu.findItem(R.id.action_share).setVisible(false); 
      return true; 
     } 

//somewhere else 

mMenu.findItem(R.id.action_share).setVisible(true); 

、あなたが

private Menu menu; 

クラスフィールドを作成し、onCreateOptionsMenuでその値を(キャプチャすることができますが@chol応答コールinvalidateOptionsMenu();

0

に基づく)

this.menu = menu; 

次に書きますclickListenerのコード

this.menu.findItem(R.id.action_share).setVisible(true); 
+0

は、同様の解決策を考え出しましたか?うーん、?? – Csabi

関連する問題