Android disable WebView touch but keep zooming/scrolling

后端 未结 4 816
误落风尘
误落风尘 2021-01-23 16:59

I am looking for a method to disable touch on a WebView so user\'s are not able to click on html links but keep the zooming and scrolling functions of the WebView.

4条回答
  •  忘了有多久
    2021-01-23 17:57

    I used rattisuk's answer at fist but realized that if you click somewhere, the click gets intercepted but turns into a long click, making the phone vibrate and also poping some potential edition menu if you clicked on some text field. Also whenever pressing on changeable UI elements in the webview (clicking on a link or a button for example), those UI elements would change appearance. To prevent that, here is my improved solution :

    // Disable the haptic feedback so that handling the long click doesnt make the phone vibrate
    webview.setHapticFeedbackEnabled(false);
    // Intercept long click events so that they dont reach the webview
    webview.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(final View v) {
            return true;
        }
    });
    // Intercept any touch event not related to scrolling/zooming
    webview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(final View v, final MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                // Change ACTION_DOWN location to outside of the webview so that it doesnt affect 
                // pressable element in the webview (buttons receiving PRESS" will change appearance)
                event.setLocation(webview.getWidth() + 1, webview.getHeight() + 1);
            }
            // Intercept any "up" event
            return event.getAction() == MotionEvent.ACTION_UP;
        }
    });
    

提交回复
热议问题