简体   繁体   English

触摸事件在android phonegap webview(甚至内置浏览器)中不起作用

[英]touch events not working in android phonegap webview (or even built-in browser)

I was using phonegap and needed to use 'touchstart' to speed up click response, only to realize that none of 'touchstart', 'touchmove', 'touchend' is firing but 'click'. 我使用的是phonegap,需要使用“ touchstart”来加快点击响应,只是意识到“ touchstart”,“ touchmove”,“ touchend”没有触发,而是“ click”触发。

I even tested a simple page in the built-in browser (7" android 4.0.3 tablet), and found it failed. 我什至在内置浏览器(7英寸android 4.0.3平板电脑)中测试了一个简单的页面,但发现它失败了。

I tested on two tablets still the same. 我测试了两种平板电脑,它们仍然相同。 In the following page, button2 shows only 'html_click', button3 shows only 'click': 在下一页中,button2仅显示“ html_click”,button3仅显示“ click”:

<!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html;charset=utf8">
    <meta name="viewport" content="target-densitydpi=device-dpi, width=device-width">
    <title>Droid touch event test.</title>
    </head>
    <body>
    <button id="button">touch it</button>
    <button id="button2" ontouchstart="d_('html_touchstart')" onclick="d_('html_click')">html</button>
    <button id="button3">touch 3</button>
    <div id="db"></div>
    <script>
            function $(d){return document.getElementById(d)}
            function d_(m){dbo.innerHTML=dbo.innerHTML+' '+m}
            function ts1(e){d_('document touched')}
            function ts2(e){d_('button touched')}
            function eh(e){
                d_(e.type);
            }
            var ets='touchstart',dbo=$('db');
            document.addEventListener(ets,ts1,false);
            $('button').addEventListener(ets,ts2,false);
            $('button3').addEventListener(ets,eh,false);
            $('button3').addEventListener('touchend',eh,false);
            $('button3').addEventListener('click',eh,false);
    </script>
    </body>
    </html>

Why 'touchstart isn't firing? 为什么'touchstart不触发? Should I do anything to make touch events work? 我应该做些什么使触摸事件起作用吗? Or is there any other way around to make click response faster? 还是有其他方法可以使点击响应更快?

I used mousedown event, worked but not notably fast. 我使用了mousedown事件,但效果很快。

(new Date()).getTime() difference (including some innerHTML change) between mousedown and click is only around 15. mousedownclick之间的(new Date()).getTime()差异(包括一些innerHTML更改)仅约15。

Keeping only what was necessary to show you the concept, I wrote this and tested using Phonegap Build (default to Phonegap 2.0, but shouldn't matter) on an ICS device. 我只保留了向您展示概念所需的内容,因此编写了该代码,并在ICS设备上使用Phonegap Build(默认为Phonegap 2.0,但没有关系)进行了测试。 Should work for you: 应该为您工作:

<!DOCTYPE html>
<html>
    <body>
        <button id="button1">1</button>
        <button id="button2">2</button>

        <script>
            document.getElementById("button1").addEventListener('touchstart',button1Pressed);
            document.getElementById("button2").addEventListener('touchstart',button2Pressed);
            function button1Pressed ()
            {
                alert('Button 1 Pressed');
            }

            function button2Pressed ()
            {
                alert('Button 2 Pressed');
            }
        </script>
    </body>
</html>

I also faced the same problem. 我也面临同样的问题。 On bellow first line i was getting error "Android WebView and NO_FAST_DRAW = false". 在下面的第一行中,我收到错误消息“ Android WebView和NO_FAST_DRAW = false”。 After change to second line. 之后换到第二行。 it worked. 有效。 the error was at createCustomAlert('wpt'). 错误发生在createCustomAlert('wpt')。 SO the function which I was called look like a string. 因此,我所谓的函​​数看起来像一个字符串。 This err was coming. 这个错误来了。 So please check at place of function calling place 所以请在功能调用处检查

onclick = 'createCustomAlert('wpt')' onclick ='createCustomAlert('wpt')'

onclick = 'createCustomAlert(3)' onclick ='createCustomAlert(3)'

I also faced the same problem and messed it with almost three days. 我也遇到了同样的问题,将近三天时间把它弄乱了。 I put click events on my html input tags, (EditText) and tried, but it also didn't work. 我在HTML输入标签(EditText)上放置了点击事件,并尝试了一下,但是也没有用。 Finally, to my miracle, I just put a simple alert("Hello") to see with ontouchstart . 最后,奇迹般的是,我只用了一个简单的alert("Hello")即可通过ontouchstart进行查看。 To my surprise, I successfully got the alert and also softkeyboard, for which I was trying almost a week. 令我惊讶的是,我成功尝试了近一个星期的警报和软键盘。 You also try with this.. Best of Luck 您也可以尝试一下。

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

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