简体   繁体   English

Webview未捕获JavaScript触摸事件

[英]Webview not catching javascript touch events

I have a web view which loads url. 我有一个加载URL的Web视图。 When url loads, there should be a reaction on touch events like touchstart, touchmove and touchend. 加载网址后,应该对诸如touchstart,touchmove和touchend之类的触摸事件做出反应。 If i try it on web browser on device alerts are fired, but if i open it in webview, there is no reaction. 如果我在网络浏览器上尝试在设备上触发警报,但是如果我在Webview中打开它,则没有任何反应。 Am i missing something? 我想念什么吗?

Here is my code: 这是我的代码:

webview.xml: webview.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutBoard"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <WebView
        android:id="@+id/wvBoard"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:clickable="false"
        android:scrollbars="none" />

</RelativeLayout>

Activity: 活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.webview);

    myWebView = (WebView) findViewById(R.id.wvBoard);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadUrl(my_url);
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.getSettings().setUseWideViewPort(false);
    myWebView.setHorizontalScrollBarEnabled(false);
    myWebView.setVerticalScrollBarEnabled(false);
    myWebView.getSettings().setLayoutAlgorithm(
        LayoutAlgorithm.SINGLE_COLUMN);

    myWebView.setInitialScale(170);
}

javascript on serverside: 服务器端的javascript:

$(document).ready(function () {
    ...

    canvas.addEventListener('touchstart', ev_canvas, false);
    canvas.addEventListener('touchmove', ev_canvas, false);
    canvas.addEventListener('touchend', ev_canvas, false);
}

function ev_canvas(ev) {
    this.touchstart = function (ev) {
        alert("called" + ev._x);
    };   
}      

Edit: I tried adding: 编辑:我尝试添加:

webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
    return super.onJsAlert(view, url, message, result);
    }
});

but nothnig changed. 但nothnig改变了。

Edit2: I noticed one line in logcat which sais: nativeOnDraw failed; Edit2:我注意到logcat的一行内容是:nativeOnDraw失败; clearing to background color. 清除为背景色。 It seems this error appears only on Android 4.4 (which is also my case) but solution in this link: WebView Rendering Issue in Android KitKat didn't help. 看来此错误仅出现在Android 4.4(这也是我的情况)上,但是此链接中的解决方案: Android KitKat中的WebView渲染问题没有帮助。

It need not be an issue with the touch events. 触摸事件不一定是问题。 It could be that the alerts are not working. 可能是警报不起作用。 Try using a WebChromeClient as suggested in this JavaScript alert not working in Android WebView stackoverflow question 按照此JavaScript警报中的建议尝试使用WebChromeClient 在Android WebView stackoverflow问题中不起作用

This happens at the last time when the webview destroyed with a alert. 这是在最后一次当Webview被警报破坏时发生的。 So when the next time web view open there are two webview instances opened. 因此,下次打开Web视图时,将打开两个WebView实例。 So when the webview destroyed we should clear one webview instance. 因此,当webview销毁时,我们应该清除一个webview实例。

@Override
    public void onDestroy() {
        super.onDestroy();
        webView.loadUrl("about:blank");
}

This was fixed mine. 这是固定的。

After being frustrated for awhile, I closed my Android app, then reopened it, and everything was working fine. 沮丧了一段时间后,我关闭了Android应用程序,然后重新打开它,一切正常。 I am not sure what caused things to stop working, but the ole' turn-it-off-and-on did the trick for me. 我不确定是什么原因导致事情停止运行,但是ole's Turn-it-on-on-on帮了我大忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM