确认对话框返回false立即
问题描述:
这是我的代码:确认对话框返回false立即
index.html:
...
<script type="text/javascript" charset="utf-8" src="main.js"></script>
</head>
<body onload="init();"></body>
...
main.js:
function onBackPressed(e) {
console.log("onBackPressed()");
var answer = confirm("Do you want to exit?");
console.log(answer);
if (answer) {
navigator.app.exitApp();
}
}
function init() {
...
document.addEventListener("backbutton", onBackPressed, false);
...
}
当我退出应用程序第一次,一切似乎是确定。 问题是我下次启动应用程序时,确认对话框立即返回false并且对话框保持可见状态。所以当我点击“确定”时,什么都不会发生。
下面是logcat的输出:
04-11 16:16:26.444: D/PhoneGapLog(18356): onBackPressed()
04-11 16:16:26.444: D/PhoneGapLog(18356): file:///android_asset/www/main.js: Line 49 : onBackPressed()
04-11 16:16:26.444: I/Web Console(18356): onBackPressed() at file:///android_asset/www/main.js:49
04-11 16:16:26.584: D/PhoneGapLog(18356): false
04-11 16:16:26.584: D/PhoneGapLog(18356): file:///android_asset/www/main.js: Line 51 : false
04-11 16:16:26.584: I/Web Console(18356): false at file:///android_asset/www/main.js:51
这是PhoneGap的或Android的错误?或者我做错了什么?
我使用Nexus One与android 2.3.6和phonegap 1.4.1(版本1.5有问题与backbutton事件)。
答
在PhoneGap中,确认是否进行异步回调。请参阅api docs。
变量答案将因为立即返回而始终为假。
代码应该看起来更像:
// process the confirmation dialog result
function onConfirm(button) {
alert('You selected button ' + button);
}
// Show a custom confirmation dialog
//
function showConfirm() {
navigator.notification.confirm(
'You are the winner!', // message
onConfirm, // callback to invoke with index of button pressed
'Game Over', // title
'Restart,Exit' // buttonLabels
);
}
显示如何onbackpressed()被执行 - 并且看看这里:http://stackoverflow.com/a/3212253/295783 – mplungjan 2012-04-11 14:43:18
我有'的onload = “init()”'在身体标记和init函数中我有'document.addEventListener(“backbutton”,onBackPressed,false);'。 onBackPressed()函数包含上面的代码。 – Chemik 2012-04-11 15:34:41