android webview: prevent text selection actionMode actionBar

你离开我真会死。 提交于 2020-01-12 18:43:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!