2014-07-26 38 views
1

我想將上下文菜單應用到一個文本框。我想設置剪切,複製,過去,當字符串剪貼板是空的,當字符串剪貼板不空,我想剪切,複製,過去被啓用。如何disble菜單項在Android中的ContextMenu

首先我將可變剪貼板分配給empy String clipboard =「」;

然後

公共無效onCreateContextMenu(文本菜單菜單,視圖V, ContextMenuInfo menuInfo){

getMenuInflater().inflate(R.menu.context, menu); 

     if(clipboard ==""){ 
     menu.findItem(R.id.action_cut).setEnabled(false); 
     menu.findItem(R.id.action_copy).setEnabled(false); 
     menu.findItem(R.id.action_paste).setEnabled(false);}else{ 
     menu.findItem(R.id.action_cut).setEnabled(true); 
     menu.findItem(R.id.action_copy).setEnabled(true); 
     menu.findItem(R.id.action_paste).setEnabled(true);} 
     super.onCreateContextMenu(menu, v, menuInfo); 
} 

,但它無法正常工作......即使clicpboard是空的,但項目仍菜單啓用。

+0

你混淆Java中的字符串匹配。它的clipboard.equals(「」)而不是==作比較 – humblerookie

回答

0

這裏是我的建議:

getMenuInflater().inflate(R.menu.context, menu); 

if("".equals(clipboard)) { // if clipboard is null this will not throw a NPE 
    menu.findItem(R.id.action_cut).setEnabled(false); 
    menu.findItem(R.id.action_copy).setEnabled(false); 
    menu.findItem(R.id.action_paste).setEnabled(false); 
} else { 
    menu.findItem(R.id.action_cut).setEnabled(true); 
    menu.findItem(R.id.action_copy).setEnabled(true); 
    menu.findItem(R.id.action_paste).setEnabled(true);} 
    super.onCreateContextMenu(menu, v, menuInfo); 
} 
0

你不能比擬的字符串值「==」必須使用相同象下面這樣:

if(clipboard.equals("")){ 
       menu.findItem(R.id.action_cut).setEnabled(false); 
       menu.findItem(R.id.action_copy).setEnabled(false); 
       menu.findItem(R.id.action_paste).setEnabled(false);} 
       } 

     else { 
      menu.findItem(R.id.action_cut).setEnabled(true); 
       menu.findItem(R.id.action_copy).setEnabled(true); 
       menu.findItem(R.id.action_paste).setEnabled(true); 
     } 
     super.onCreateContextMenu(menu, v, menuInfo); 
     } 
+0

非常感謝你! – user3785958

+0

@ user3785958歡迎您。如果問題解決了,請選擇最佳答案。祝你好運 – meysam

相關問題