问题
I want to preserve the text selection in a webView while preventing any contextual menu of the actioMode from being shown. Neither the new floating one nor the old actionBar, just the selection handles and of course the selection behaviour.
Hooking to the actionModeCallback in startActionMode allows me to clear all items in the menu in the callback onCreateActionMode.
This works fine on android 6, since an empty floating menu won't show at all, and the actionMode text selection behaviour is preserved.
Unfortunately on android < 6 this leaves an empty actionBar shown, how can I completely remove it?
Is there a clean way to obtain this? I'm working in a ReactNative app, but using a custom extended webview, and I have also access to mainActivity code.
回答1:
Indeed it was pretty straightforward: return a new empty ActionMode in startActionMode instead of returning null, null being interpreted as cancel the action mode and what called for it.
You'll need to override both the old and the new api 23 signature of startActionMode:
@Override
public ActionMode startActionMode(ActionMode.Callback callback, int type) {
return this.dummyActionMode();
}
@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
return this.dummyActionMode();
}
public ActionMode dummyActionMode() {
return new ActionMode() {
@Override public void setTitle(CharSequence title) {}
@Override public void setTitle(int resId) {}
@Override public void setSubtitle(CharSequence subtitle) {}
@Override public void setSubtitle(int resId) {}
@Override public void setCustomView(View view) {}
@Override public void invalidate() {}
@Override public void finish() {}
@Override public Menu getMenu() { return null; }
@Override public CharSequence getTitle() { return null; }
@Override public CharSequence getSubtitle() { return null; }
@Override public View getCustomView() { return null; }
@Override public MenuInflater getMenuInflater() { return null; }
};
}
If you like java you may want to change the public signature and indentation for the dummyActionMode function, I don't.
来源:https://stackoverflow.com/questions/36088057/android-webview-prevent-text-selection-actionmode-actionbar