Android禁用WebView触摸但保持缩放/滚动

时间:2012-09-16 22:20:33

标签: android webview scroll touch zoom

我正在寻找一种方法来禁用WebView上的触摸,因此用户无法点击html链接,但保留了WebView的缩放和滚动功能。

4 个答案:

答案 0 :(得分:2)

下面的代码允许您滚动,但点击链接时它不会转到下一页。

webView.setLongClickable(false);

webView.setOnTouchListener(new View.OnTouchListener()
{

  @Override

  public boolean onTouch(View arg0, MotionEvent arg1)
  {

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

  }
}

);

答案 1 :(得分:1)

Webview.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {      
                    return false;
        }
});

答案 2 :(得分:1)

我在第一时间使用了rattisuk的答案,但意识到如果你点击某个地方,点击会被拦截,但会变成长按,让手机振动,如果点击某个文字字段,还会弹出一些潜在的编辑菜单。 此外,每当按下webview中的可更改UI元素(例如,单击链接或按钮)时,这些UI元素将改变外观。 为了防止这种情况,这是我改进的解决方案:

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

答案 3 :(得分:0)

这是我的解决方案。它不会让用户触发javascript点击事件

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