Android WebView:防止文本选择actionMode actionBar [英] android webview: prevent text selection actionMode actionBar

查看:433
本文介绍了Android WebView:防止文本选择actionMode actionBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在webView中保留文本选择,同时防止显示actioMode的任何上下文菜单。无论是新的浮动控件还是旧的actionBar,都不会处理选择内容,当然也不会处理选择行为。



在startActionMode中切换到actionModeCallback允许我清除



这在android 6上很好用,因为根本不会显示一个空的浮动菜单,并且保留了actionMode文本选择行为。 / p>

不幸的是,在android< 6这样会显示一个空的actionBar,如何将其完全删除?



是否有一种干净的方法来获取它?我正在使用ReactNative应用程序,但使用的是自定义扩展的Webview,并且还可以访问mainActivity代码。

解决方案

确实,这非常简单:在startActionMode中返回一个新的空ActionMode而不是返回null,null解释为取消操作模式及其要求。



需要重写startActionMode的新旧API 23签名:

  @Override 
public ActionMode startActionMode(ActionMode .Callback callback,int type){
返回this.dummyActionMode();
}

@Override
public ActionMode startActionMode(ActionMode.Callback callback){
返回this.dummyActionMode();
}

public ActionMode dummyActionMode(){
return new ActionMode(){
@Override public void setTitle(CharSequence title){}
@Override公共无效setTitle(int resId){}
@Override公共无效setSubtitle(CharSequence字幕){}
@Override公共无效setSubtitle(int resId){}
@Override公共无效setCustomView(查看查看} {}
@Override public void invalidate(){}
@Override public void finish(){}
@Override public Menu getMenu(){返回null; }
@Override public CharSequence getTitle(){返回null; }
@Override public CharSequence getSubtitle(){返回null; }
@Override public View getCustomView(){返回null; }
@Override public MenuInflater getMenuInflater(){返回null; }
};
}

如果您喜欢Java,则可能希望更改其公共签名和缩进dummyActionMode函数,我不知道。


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.

解决方案

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.

这篇关于Android WebView:防止文本选择actionMode actionBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆