Android:禁用webview中的文本选择

jan*_*ver 52 android webview

我正在使用webview在我的应用程序中呈现一些格式化的东西.对于某些交互(特定于某些dom元素),我使用javascript和WebView.addJavascriptInterface().现在,我想要认识一下.不幸的是,onLongTouch在Android 2.3中,显示了文本选择的句柄.

如何在设置onTouchListener和返回true的情况下关闭此文本选择?(然后,与"网站"的互动不再起作用.

Sam*_*uel 116

这对我有用

mWebView.setOnLongClickListener(new OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        return true;
    }
});
mWebView.setLongClickable(false);
Run Code Online (Sandbox Code Playgroud)

我没有测试过,如果你不想长按一下引起振动,你可以试试这个:

mWebView.setHapticFeedbackEnabled(false);
Run Code Online (Sandbox Code Playgroud)

  • mWebView.setLongClickable(假); 没有任何不同,但设置长点击监听器并从onLongClick()方法传递true对我有用. (3认同)
  • 这工作正常,但如果我们双击文本字段中的内容,则会启用选择和复制选项。如何禁用此功能? (3认同)
  • 这对我有用,但我不需要最后一行"mWebView.setLongClickable(false);" (2认同)

小智 31

设置WebKit的CSS属性-webkit-user-selectnone可以解决这个问题.

用于禁用选择的示例CSS:

* {
   -webkit-user-select: none;
}
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这并不适用于所有版本的Android.它_should_是最好的解决方案,但它不是:/ (5认同)
  • *:not(输入):not(textarea){webkit-user-select:none; } (2认同)
  • 非常感谢,为了让它与 android 一起工作,我将其更改为 '{ webkit-user-select: none; 用户选择:无;}' (2认同)

jan*_*ver 9

我想到了!!这就是你如何实现自己的longtouchlistener.在函数longTouch中,您可以调用javascript界面​​.

var touching = null;
$('selector').each(function() {
    this.addEventListener("touchstart", function(e) {
        e.preventDefault();
        touching = window.setTimeout(longTouch, 500, true);
    }, false);
    this.addEventListener("touchend", function(e) {
        e.preventDefault();
        window.clearTimeout(touching);
    }, false);
});

function longTouch(e) {
    // do something!
}
Run Code Online (Sandbox Code Playgroud)

这有效.


小智 5

如果您使用,似乎关闭通过长按切割/粘贴

    articleView.setWebChromeClient(new WebChromeClient(){...})
Run Code Online (Sandbox Code Playgroud)

请参阅https://bugzilla.wikimedia.org/show_bug.cgi?id=31484

因此,如果您使用setChromeClient并且您想要长时间单击以开始复制/粘贴,请执行以下操作:

    webView.setWebChromeClient(new WebChromeClient(){

        [.... other overrides....]

        // @Override
        // https://bugzilla.wikimedia.org/show_bug.cgi?id=31484
        // If you DO NOT want to start selection by long click,
        // the remove this function
        // (All this is undocumented stuff...)
        public void onSelectionStart(WebView view) {
            // By default we cancel the selection again, thus disabling
            // text selection unless the chrome client supports it.
            // view.notifySelectDialogDismissed();
        }

    });
Run Code Online (Sandbox Code Playgroud)