【发布时间】:2014-07-27 22:05:19
【问题描述】:
我有一个加载 url 的 web 视图。当 url 加载时,应该对 touchstart、touchmove 和 touchend 等触摸事件有反应。如果我在设备警报上的网络浏览器上尝试它,但如果我在 webview 中打开它,则没有反应。我错过了什么吗?
这是我的代码:
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>
活动:
@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:
$(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);
};
}
编辑: 我尝试添加:
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
但没有任何改变。
编辑2: 我注意到 logcat 中有一行 sais: nativeOnDraw failed;清除为背景颜色。 似乎此错误仅出现在 Android 4.4(这也是我的情况)上,但此链接中的解决方案:WebView Rendering Issue in Android KitKat 没有帮助。
【问题讨论】:
标签: javascript android webview