1

我正在寻找一种禁用 WebView 触摸的方法,以便用户无法单击 html 链接,但保留 WebView 的缩放和滚动功能。

4

4 回答 4

2

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

webView.setLongClickable(false);

webView.setOnTouchListener(new View.OnTouchListener()
{

  @Override

  public boolean onTouch(View arg0, MotionEvent arg1)
  {

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

  }
}

);
于 2012-12-27T10:54:53.393 回答
1

我一开始就使用了rattisuk的答案,但意识到如果你点击某个地方,点击会被拦截,但会变成长按,如果你点击了某个文本字段,手机会振动并且还会弹出一些潜在的版本菜单。此外,只要在 web 视图中按下可更改的 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;
    }
});
于 2014-07-04T20:28:48.937 回答
1
Webview.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {      
                    return false;
        }
});
于 2012-09-17T00:37:04.107 回答
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;
        }
    }
}
于 2015-09-03T10:11:22.440 回答