Android disable WebView touch but keep zooming/scr

2019-03-01 07:51发布

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条回答
2楼-- · 2019-03-01 08:00

the code below allow you scroll but when click at the link it won't go to next page.

webView.setLongClickable(false);

webView.setOnTouchListener(new View.OnTouchListener()
{

  @Override

  public boolean onTouch(View arg0, MotionEvent arg1)
  {

       return arg1.getAction() == MotionEvent.ACTION_UP;

  }
}

);
查看更多
Viruses.
3楼-- · 2019-03-01 08:13
Webview.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {      
                    return false;
        }
});
查看更多
霸刀☆藐视天下
4楼-- · 2019-03-01 08:20

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;
    }
});
查看更多
女痞
5楼-- · 2019-03-01 08:21

Here is my solution. It does not let a user trigger javascript click events

public class NonClickableWebView extends WebView {
    private GestureDetectorCompat gestureDetector;
    private boolean isSingleTap = false;

    public NonClickableWebView(Context context) {
        super(context);
        gestureDetector = new GestureDetectorCompat(context, new GestureDetector());
    }

    public NonClickableWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        gestureDetector = new GestureDetectorCompat(context, new GestureDetector());
    }

    public NonClickableWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetectorCompat(context, new GestureDetector());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            isSingleTap = false;
        }
        gestureDetector.onTouchEvent(event);

        if (isSingleTap && event.getAction() == MotionEvent.ACTION_UP) {
            return true;
        }

        return super.onTouchEvent(event);
    }

    private class GestureDetector extends android.view.GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            isSingleTap = true;
            return false;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            isSingleTap = true;
            return false;
        }
    }
}
查看更多
登录 后发表回答