Android: Disable text selection in a webview

后端 未结 7 661
误落风尘
误落风尘 2020-11-27 11: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.addJavas

相关标签:
7条回答
  • 2020-11-27 12:18

    For kotlin i found the following to work:

    webView.isLongClickable = false
    
    0 讨论(0)
  • 2020-11-27 12:19

    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;
       }
    }
    
    0 讨论(0)
  • 2020-11-27 12:24

    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();
            }
    
        });
    
    0 讨论(0)
  • 2020-11-27 12:31

    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.

    0 讨论(0)
  • 2020-11-27 12:32

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

    Example CSS to disable selection:

    * {
       -webkit-user-select: none;
    }
    
    0 讨论(0)
  • 2020-11-27 12:41

    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);
    
    0 讨论(0)
提交回复
热议问题