我正在尝试使用HTML5画布和触控功能在Android上制作一个WebView应用程序。
以下是我认为代码相关部分:
在onCreate方法中:
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
...
myWebView.loadData(content,"text/html", "UTF-8");
如果字符串“content”包含一段HTML / JavaScript代码,其中包含以下行。
this.canvas = document.getElementById(AN_ID); // AN_ID is id of a canvas tag
...
this.canvas.addEventListener("touchstart", function(e) {
...
}, false);
this.canvas.addEventListener("touchend", function(e) {
...
}, false);
而 activity_main.xml 文件包含标签
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
然而,我的测试设备 Nexus 7 似乎无法响应触摸事件。如果我将 "touchstart" 和 "touchend" 替换为 "mousedown" 和 "mouseup",那么它似乎可以识别 mousedown 事件。但是 mouseup 没有像在浏览器中运行的相同代码一样正常工作。我是否明显地忽略了某些东西,例如需要在 JavaScript 中启用触摸事件的配置设置?