如何在android中取消ContextMenu的菜单项
我想将上下文菜单应用于一个文本视图。当字符串剪贴板为空时,我想将剪切、复制、粘贴设置为禁用,当字符串剪贴板不为空时,我想将剪切、复制、粘贴设置为启用 首先,我将变量剪贴板指定为空如何在android中取消ContextMenu的菜单项,android,menu,Android,Menu,我想将上下文菜单应用于一个文本视图。当字符串剪贴板为空时,我想将剪切、复制、粘贴设置为禁用,当字符串剪贴板不为空时,我想将剪切、复制、粘贴设置为启用 首先,我将变量剪贴板指定为空 String clipboard = ""; 然后 但它不起作用。。。即使剪贴板是空的,但“项目”菜单仍处于启用状态。以下是我的建议: getMenuInflater().inflate(R.menu.context, menu); if("".equals(clipboard)) { // if clipboa
String clipboard = "";
然后
但它不起作用。。。即使剪贴板是空的,但“项目”菜单仍处于启用状态。以下是我的建议:
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);
}
不能将字符串值与==进行比较,必须使用以下等式:
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);
}
您混淆了java中的字符串匹配。它的clipboard.equals和not==用于comparison@user3785958欢迎光临。如果问题解决了,选择最佳答案。祝你好运
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);
}