2013-05-10 67 views
0

我有一个jQuery移动应用程序(约7个HTML页面)。我想更改Android中的后退按钮功能(使用带Phonegap的Eclipse)。我尝试了所有@overwrride,但它不能正常工作。jQuery Mobile - Android后退按钮提示确认

我想在每一页上,当后退按钮被击中时,得到一个提示:“你确定要退出吗?”是和没有选项。

现在我有这个脚本完全退出应用程序。

<script type="text/javascript"> 
    document.addEventListener("deviceready", onDeviceReady, false); 
    function onDeviceReady() { 
    document.addEventListener("backbutton", onBackKeyDown, false); 
    } 
    function onBackKeyDown() { 
    navigator.app.exitApp(); 
    } 
</script> 

有没有办法有功能onBackKeyDown()内jQuery的窗口弹出,并有一个if条件,在那里你点击YES执行navigator.app.exitApp();或取消,如果你不打?

我对此很陌生,所以我非常感谢您的帮助!

谢谢。

回答

0

如果我理解你的问题的权利,你可以这样做:

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. 
    } 
} 
+0

非常感谢您的帮助!我添加了代码,但后退按钮现在让我回到上一页。在Eclipse的LogCat中我得到的错误:无法添加窗口 - 令牌android.os.BinderProxy无效;你的活动正在运行? 你有什么建议我需要添加什么?除活动屏幕外,活动文件中没有任何内容,并且在清单中我只需要对互联网和网络的许可。 在此先感谢!!!!! – user2370024 2013-05-10 19:27:03

+0

@ user2370024是否使用exitApp()函数工作? – 2013-05-10 20:07:10

+0

是的,我发布的上面的代码工作正常。它在任何页面上退出应用程序,它不会返回... – user2370024 2013-05-10 21:04:26