2013-05-14 153 views
0

我想知道是否有一種方法來打開一個上下文菜單欄,單擊Android中的電話菜單按鈕。我根據這個基礎教程創建一個上下文菜單欄:tutorial contextual menu bar菜單點擊打開一個上下文菜單欄

而且我現在已經在我的活動如下:

mCallback = new Callback(){ 

    @Override 
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
     switch(item.getItemId()){ 
     case 1: 
      Toast.makeText(getBaseContext(), "Selected Action1 " + postId, Toast.LENGTH_SHORT).show(); 
      //mode.finish(); // Automatically exists the action mode, when the user selects this action 

      break; 
     case 2: 
      Toast.makeText(getBaseContext(), "Selected Action2 " + postId, Toast.LENGTH_SHORT).show(); 
      //mode.finish(); // Automatically exists the action mode, when the user selects this action 
      break; 
     case 3: 
      Toast.makeText(getBaseContext(), "Selected Action3 " + postId, Toast.LENGTH_SHORT).show(); 
      //mode.finish(); // Automatically exists the action mode, when the user selects this action 
      break; 

     } 
     return false; 

    } 

    @Override 
    public boolean onCreateActionMode(ActionMode mode, Menu menu) { 
     mode.setTitle("Actions for Post"); 
     getMenuInflater().inflate(R.menu.context_menu_posts, menu); 

     menu.add(1, 1, 1, "Option 1"); 
     menu.add(1, 2, 2, "Option 2"); 
     menu.add(1, 3, 3, "Option 3"); 



     return true; 
    } 

    @Override 
    public void onDestroyActionMode(ActionMode mode) { 
     mMode = null; 

    } 

    @Override 
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
     return false; 
    } 

}; 

但現在我希望它「秀」,當用戶點擊菜單按鈕。之前,我有這個代碼在LongclickListener:

if(mMode!=null) 
     return false; 
    else 
     mMode = startActionMode(mCallback); 

這在活動控制工作很大,但有辦法的菜單中點擊做到這一點?我嘗試將@Averriden菜單方法中的startActionMode代碼放入,但我無法在單擊菜單按鈕時打開它。

在此先感謝!

回答

0

對不起,愚蠢的職位,我一定比我想象的更累。

是不是認爲它只是覆蓋menubutton並在那裏添加代碼,因爲我以前沒有使用它。

此貼已解決:Intercept menuclick

相關問題