简体   繁体   English

Android Webview:JavaScript停止运行

[英]Android webview: JavaScript halts on touch

I'm trying to use a webview to display a page that does some very basic canvas animations and displays the current time as a string. 我正在尝试使用网络视图来显示一个页面,该页面执行一些非常基本的画布动画并将当前时间显示为字符串。 I use setTimeout() to update the content once per second (or 30 times per second for the animation). 我使用setTimeout()每秒更新一次内容(或动画每秒更新30次)。

My problem is that as soon as I touch the screen, my javascript stops executing. 我的问题是,一旦我触摸屏幕,我的JavaScript就会停止执行。

The furthest I have got was adding listeners for touch events to my javascript and call e.preventDefault() in the event handler, which helped in most cases, but seemed to miss some cases for where none of the usual javascript events got fired. 我得到的最远的是将触摸事件的侦听器添加到我的javascript中,并在事件处理程序中调用e.preventDefault(),这在大多数情况下都很有帮助,但似乎错过了某些情况,因为这些事件没有触发任何常规的javascript事件。

The next thing I tried was to override the default touch handler as follows: 我尝试的下一件事是重写默认的触摸处理程序,如下所示:

myWebView.setOnTouchListener(new View.OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    return true;
  }
});

This helped, but the problem is that it consumes the touch event before it gets dispatched to my webview, making it impossible to listen for touch events in javascript. 这有所帮助,但是问题是它在将触摸事件发送到我的Webview之前就消耗了触摸事件,从而无法在javascript中侦听触摸事件。

I want to disable all the default Android behavior like zooming and so on, since this is what blocks my animation, but I still want to be able to use these events in javascript. 我想禁用所有默认的Android行为(例如缩放等),因为这会阻止我的动画,但是我仍然希望能够在javascript中使用这些事件。

Any ideas? 有任何想法吗?

Yes, return false. 是的,返回false。 If you return true, you "say" the event handled. 如果返回true,则“说”处理的事件。

http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent) http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent)

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

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