4

我有一个 web 视图,我正在设置一个 onTouchListener 以捕获实际视图上的滑动事件。

我还在 WebView 上设置了 WebViewClient,以便在单击 webview 中的某些链接时覆盖 Url Loading。

问题是 OnTouch 处理程序总是首先接收操作,即使我返回 false(当不是滑动时)它也不会将触摸事件传递给内部 html 内容,因此实际上从未点击过链接。如果我删除 onTouchListener 它工作正常。是否可以以某种方式将触摸事件传递给内容?

4

2 回答 2

7

您需要在主活动的 OnCreate() 中设置一个带有 requestFocus() 的 OnTouchListener():

mWebView = (MyWebView) findViewById(R.id.webview);

mWebView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                if (!v.hasFocus()) {
                    v.requestFocus();
                }
                break;
        }
        return false;
    }
});
于 2013-10-29T00:50:59.800 回答
1

不要使用 OnTouchListener,而是在 WebView 中覆盖 OnTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent event) {

    // do your stuff here... the below call will make sure the touch also goes to the webview.

    return super.onTouchEvent(event);
}
于 2014-03-11T10:11:49.100 回答