Android的禁用的WebView联系,但保持缩放/滚动(Android disable WebV

2019-07-31 14:57发布

我在寻找一种方法来禁用一个的WebView联系,以便用户不能点击HTML链接,但保留缩放和网页视图的滚动功能。

Answer 1:

下面的代码允许您滚动但是当点击该链接就不会去下一个页面。

webView.setLongClickable(false);

webView.setOnTouchListener(new View.OnTouchListener()
{

  @Override

  public boolean onTouch(View arg0, MotionEvent arg1)
  {

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

  }
}

);


Answer 2:

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


Answer 3:

我用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;
    }
});


Answer 4:

这里是我的解决方案。 它不会让用户触发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;
        }
    }
}


文章来源: Android disable WebView touch but keep zooming/scrolling