Aeson
Aeson
  • 发布:2016-04-07 15:50
  • 更新:2019-10-14 23:13
  • 阅读:4476

plus怎么阻止安卓默认返回按钮的事件?

分类:Native.js

我按照hello h5+ 的源代码。

        document.addEventListener('touchstart', function() {  
            return false;  
        }, true);  
        var as = 'pop-in';  
        function plusReady() {  
            plus.webview.currentWebview().setStyle({  
                scrollIndicator: 'none'  
            });  
            plus.key.addEventListener('backbutton', eventBackButton, false);  
        }  

        function eventBackButton() {  
            if (confirm('确认退出?')) {  
                plus.runtime.quit();  
            }  
        }  
        if (window.plus) {  
            plusReady();  
        } else {  
            document.addEventListener('plusready', plusReady, false);  
        }  

这样写进项目里面,安卓上测试点击弹框的取消按钮以后,APP还是退出了,但是hello h5+却不会退出,是不是我没有屏蔽安卓的退出默认事件呢?还是其他?

请问怎么解决呢?

2016-04-07 15:50 负责人:无 分享
已邀请:
然后去远足

然后去远足

这是很基本的事件处理问题了,在 JS 中,addEventListener() 只会增加新的执行程序,并不会覆盖原有的。

H5Plus 的 DEMO 里人家这么写是人家没引用 mui.js。你的项目里用了 MUI,它已经监听过一次 backbutton 事件了,你这么写并不能覆盖原有的事件。

如果要自定义返回逻辑,需要重写 mui.back,而不是通过 addEventListener() 添加 backbutton 事件的监听。

Aeson

Aeson (作者)

为什么这样写他也能直接退出呢

if (confirm('确认退出?')) {

}

JackLondon

JackLondon

<!DOCTYPE html>
<html>

<head>  
    <meta charset="utf-8">  
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />  
    <title>hello world</title>  
    <script src="js/mui.min.js"></script>  
    <script type="text/javascript">  
        (function() {  
            var first = null;  
            mui.back = function() { //首次按键,提示‘再按一次退出应用’  
                if (!first) {  
                    first = new Date().getTime();  
                    mui.toast('再按一次退出应用');  
                    setTimeout(function() {  
                        first = null;  
                    }, 1000);  
                } else {  
                    if (new Date().getTime() - first < 1000) {  
                        plus.runtime.quit();  
                    }  
                }  
            };  
        })();  
    </script>  
</head>  

<body>  
        测试返回  
</body>  

</html>

JackLondon

JackLondon

把mui.min.js引入下就行了

  • Aeson (作者)

    谢谢,这个有效!因为项目中使用angular,使用官方的hello h5+的demo代码不起作用。你说的这个有效。

    2016-04-07 18:02

  • JackLondon

    嗯,有效就好。

    2016-04-07 18:17

该问题目前已经被锁定, 无法添加新回复