touchend事件不会在Android上工作会在、事件、工作、touchend

2023-09-12 08:39:00 作者:恋你√痴迷

我刚刚开始看做对Android的一些基本的移动Web开发和编写测试脚本调查触摸事件。我已经运行下面的code在Android模拟器,和touchend事件永远不会被解雇。谁能告诉我为什么吗?

I've just started looking at doing some basic mobile web development on the android and an writing a test script to investigate the touch events. I've run the following code in the android emulator, and the touchend event never gets fired. Can anyone tell me why ?

我已经试过三个版本的模拟器(1.6,2.1和2.2)和所有三个的行为以同样的方式的。

I've tried in three versions of the emulator (1.6, 2.1 and 2.2) and all three behave in the same way.

在此先感谢任何帮助,您可以给我。

Thanks in advance for any help you can give me.

干杯, 科尔姆

编辑 - 我以前也试过这种使用XUI框架,并有同样的问题,所以我猜我有多么这东西的作品一个根本性的误解......

EDIT - I've also tried this using the XUI framework and have the same problem so I'm guessing I have a fundamental misunderstanding of how this stuff works ......

             地图测试         

Map Test

    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <meta name="language" content="english" />

    <meta name="viewport" content="minimum-scale=1.0,
                                   width=device-width,
                                   height=device-height,
                                   user-scalable=no">
    <script type="text/javascript">
        window.onload = function(){
            document.body.appendChild(
                    document.createTextNode("w: " + screen.width + " x " + "h : " +screen.height)
            );
           attachTouchEvents();
        }
        function attachTouchEvents() {
            console = document.getElementById("console");
            var map = document.getElementById("map");
            map.addEventListener ('touchstart', function (event) {
                event.preventDefault();
                var touch = event.touches[0];
                document.getElementById("touchCoord").innerHTML = "S : " + touch.pageX + " " + touch.pageY;
                document.getElementById("touchEvent").innerHTML = "Touch Start";
            }, false);

            map.addEventListener ('touchmove', function (event) {
                event.preventDefault();
                var touch = event.touches[0];
                document.getElementById("touchCoord").innerHTML = "M : " + touch.pageX + " " + touch.pageY;
                document.getElementById("touchEvent").innerHTML = "Touch Move";
            }, false);

            map.addEventListener ('touchend', function (event) {
                var touch = event.touches[0];
                document.getElementById("touchCoord").innerHTML = "E : " + touch.pageX + " " + touch.pageY;
                document.getElementById("touchEvent").innerHTML = "Touch End";
                event.preventDefault();
            }, false);
            console.innerHTML = "event attached";
        }
    </script>
    <style type="text/css">
        html, body {
            height:100%;
            width:100%;
            margin: 0;
            background-color:red;
        }
        #map {
            height: 300px;
            width: 300px;
            background-color:yellow;
        }
    </style>
</head>

<body>
    <div id="map"></div>
    <div id="touchCoord">Touch Coords</div>
    <div id="touchEvent">Touch Evnt</div>
    <div id="console">Console</div>

</body>

推荐答案

对于任何人试图弄清楚为什么touchend事件缺少在Android V3和V4(也许还V2.3),有一个长期悬而未决的错误,这错误而已得到固定在V4.1(显然):

For anyone trying to figure out why touchend events are missing on Android v3 and v4 (maybe also v2.3), there is a long outstanding bug which only just got fixed in v4.1 (apparently):

http://$c$c.google.com/p/android/issues/detail?id=4549

http://$c$c.google.com/p/android/issues/detail?id=19827

要解决这个错误,你必须调用 preventDefault()在任一touchstart或第一touchmove事件。当然,这prevents本土滚动,所以你将需要重新实现自己。

To workaround this bug you have to call preventDefault() on either the touchstart or first touchmove event. Of course, this prevents the native scrolling, so you will need to re-implement that yourself.