繁体   English   中英

科尔多瓦android backbutton确认消息

[英]Cordova android backbutton confirmation message

我正在与Monaca和onsen.ui一起使用基于Cordova的vue.js相关应用程序。 我需要使用Android的“后退”按钮,以便每当用户按下手机的后退按钮时,它都会向我显示一条确认消息。 它必须显示两个选项“是”和“否”。 如果用户按“是”,则用户将离开应用程序,否则将留在应用程序中。 随着我对Vue.js的了解,Plese帮助我找到了在vue.js中实现该解决方案的解决方案。 我尝试了StackOverflow中提供的其他解决方案,但似乎没有任何效果。 感谢您的帮助。

mounted(){
            document.addEventListener('backbutton', this.onBackKeyDown, false);

        },
methods{
 onBackKeyDown:  function  (e) {
                    e.preventDefault();
                    alert('Back Button is Pressed!');
                    navigator.notification.confirm("Are you sure you want to exit ?",this.onConfirm(), "Confirmation", "Yes,No");
                    // Prompt the user with the choice
                },
                onConfirm: function (button) {
                    if (button === 2) {
                        return;
                    } else {
                        navigator.app.exitApp();
                    }
                },
}

您使用哪个js都没关系。 如果要实现上述要求,则需要安装以下插件。

cordova plugin add cordova-plugin-dialogs

和示例代码如下

document.addEventListener("deviceready", onDeviceReady, false);
 function onDeviceReady(){
         document.addEventListener("backbutton", function(e){
        navigator.notification.confirm("Are you sure you want to exit the application?",fnLogout,"Warning","Ok,Cancel"); // u can change the button names in the place of ok,cancel.
    }, false); 
}

function fnLogout(button) {
    if(button == 1) {
        navigator.app.exitApp(); //exit from the if presses "ok"
    } else {
        return; //No action if presses "cancel"
    }                     
 }

暂无
暂无

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

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