开发者

Android: Disable text selection in a webview

开发者 https://www.devze.com 2023-02-13 18:42 出处:网络
I am using a webview to present some formatted stuff in my app. For some interaction (which are specific to certain dom elements) I use javascript and WebView.addJa开发者_StackOverflow社区vascriptInte

I am using a webview to present some formatted stuff in my app. For some interaction (which are specific to certain dom elements) I use javascript and WebView.addJa开发者_StackOverflow社区vascriptInterface(). Now, I want to recognize a long touch. Unfortunately, onLongTouch, in Android 2.3 the handles for text selection are displayed.

How can I turn off this text selection without setting the onTouchListener and return true? (Then, the interaction with the "website" doesn't work anymore.


This worked for me

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

I have not tested, if you don't want the vibration caused by the long click, you can try this:

mWebView.setHapticFeedbackEnabled(false);


Setting webkit css property -webkit-user-select to none would solve the problem.

Example CSS to disable selection:

* {
   -webkit-user-select: none;
}


I figured it out!! This is how you can implement your own longtouchlistener. In the function longTouch you can make a call to your javascript interface.

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!
}

This works.


It appears that cut/paste via long press is turned off if you used

    articleView.setWebChromeClient(new WebChromeClient(){...})

See https://bugzilla.wikimedia.org/show_bug.cgi?id=31484

So if you are using setChromeClient and you WANT to have long click to start copy/paste, the do the following:

    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();
        }

    });


An alternative solution is to subclass WebView and Override performLongClick as bellow:

public class AdvanceWebView extends WebView {
   //Add constructors...
   @Override
   public boolean performLongClick() {
   return true;
   }
}


It seems that the only option is to set onTouchListener and write your own code to detect long-click. Then return true if it's a long-click and false otherwise.


For kotlin i found the following to work:

webView.isLongClickable = false
0

精彩评论

暂无评论...
验证码 换一张
取 消