我有一个可选择的TextView.我想防止在选择文本时显示上下文菜单。
我实现了这个代码,但动作栏和上下文菜单仍然显示。
tv.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Return true to ensure the yourTextView is still selectable
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Remove all action items to provide an actionmode-less selection
menu.clear();
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
}
});
如何才能永远删除它?
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
menu.removeItem(android.R.id.selectAll);
menu.removeItem(android.R.id.cut);
menu.removeItem(android.R.id.copy);
menu.removeItem(android.R.id.paste);
menu.removeItem(android.R.id.replaceText);
menu.removeItem(android.R.id.shareText);
return true;
}
尝试手动从菜单中删除项目。我希望现在应该可以了。
我想了想,最后写了下面的代码。
tv.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
removeActionBar(getWindow().getDecorView());
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode actionMode) {
}
});
private void removeActionBar(View v) {
ViewGroup viewgroup = (ViewGroup) v;
for (int i = 0; i < viewgroup.getChildCount(); i++) {
View v1=viewgroup.getChildAt(i);
if ( v1 instanceof ViewGroup )
removeActionBar(v1);
if ( v1 instanceof ActionBarContextView ) {
viewgroup.removeView(v1);
}
}
}