繁体   English   中英

在phonegap应用程序中退出时显示确认框(硬件按钮)

[英]Show confirmation box on exit(hardware button) in phonegap app

我正在在phonegap上使用iframe android应用程序。 我使用phonegap构建来在线编译代码。 一天,我正在寻找一个代码,该代码弹出确认框,显示“您要退出吗?” 是|否? 我给出了这些代码,但是它不起作用,你能帮忙吗? 我们必须在config.xml中添加任何插件吗?

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test Layout</title>
        <script type="text/javascript" charset="utf-8">

            document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button
}
function onBackKeyDown(e) {
    e.preventDefault();
    navigator.notification.confirm("Are you sure you want to exit ?", onConfirm, "Confirmation", "Yes,No"); 
    // Prompt the user with the choice
}
function onConfirm(button) {
    if(button==2){//If User selected No, then we just do nothing
        return;
    }else{
        navigator.app.exitApp();// Otherwise we quit the app.
    }
}
        </script>
        <style type="text/css">
            body, html
            {
                margin: 0; padding: 0; height: 100%; overflow: hidden;
            }
            #content
            {
                position:absolute; left: 0; right: 0; bottom: 0; top: 0px; 
            }
        </style>

    </head>
    <body>
        <div id="content">
            <iframe width="100%" height="100%" frameborder="0" src="#" />
        </div>
    </body>
</html>

对于Apache科尔多瓦/ PhoneGap的,你不需要为本地事件(如任何插件devicereadybackbutton ),但你需要的cordova-plugin-dialogs 插件 ,显示本地警报。

因此,我猜测您的问题不是捕获事件,而是显示警报。

请尝试仅打印经​​典的window.alert() ,以验证事件是否被捕获,然后您可以尝试使用对话框插件。

我认为您的代码有两个更改

首先,如果您尚未添加cordova.js则请将其添加到文件的标题部分

其次,在调用backbutton事件之前,您需要花一些时间来完成页面的加载,否则它将无法理解backbutton click事件

所以在这里我只是将超时添加到您的函数中

function onDeviceReady() {
    setTimeout(function(){
        document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button
    },500);
}

这也是我的工作代码。

$(document).ready(function(){
    setTimeout(function(){
        document.addEventListener('backbutton', function(e){    
            if (confirm("Are you sure you want to exit app?"))
            {
                navigator.app.exitApp();
            }   
        }, false);
    }, 500);
}

这是我按下后退按钮时的工作代码,它将要求退出

document.addEventListener("backbutton", function (e) {
    e.preventDefault();
    navigator.notification.confirm("Are you sure want to exit from App?", onConfirmExit, "Confirmation", "Yes,No");
}, false);


function onConfirmExit(button) {
    if (button == 2) { //If User select a No, then return back;
        return;
    } else {
        navigator.app.exitApp(); // If user select a Yes, quit from the app.
    }
}

暂无
暂无

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

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