gpt4 book ai didi

javascript - 如何使用 cordova 处理混合移动应用程序的后退按钮功能

转载 作者:行者123 更新时间:2023-11-28 10:48:38 25 4
gpt4 key购买 nike

我正在使用 cordova 创建混合移动应用程序,对此我是新手。

如果用户在主页中单击后退按钮两次,我想退出 Android 应用程序,但我无法处理后退按钮功能。

谁能告诉我如何处理特定页面的后退按钮功能

这是我的index.html

<!DOCTYPE html>

<html>
<head>

<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<title>Hello World</title>
</head>
<body>
<div id="main" data-role="page">
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>

这是我的 Index.js:

var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},

bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.addEventListener("backbutton", function() {
if ( $('.ui-page-active').attr('id') == 'main') {
exitAppPopup();
} else {
history.back();
}
}, false);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');

listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');

console.log('Received Event: ' + id);
},
function exitAppPopup() {
navigator.notification.confirm(
'Exit PhoneGap ' + device.cordova + ' Demo?'
, function(button) {
if (button == 2) {
navigator.app.exitApp();
}
}
, 'Exit'
, 'No,Yes'
);
return false;
}
};

app.initialize();

最佳答案

<script type="text/javascript" src="cordova.js"></script>
<script>
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
if (navigator.userAgent.match("Android"))
{
document.addEventListener("backbutton", onBackKeyDown, true);
}
}
function onBackKeyDown(e)
{
if (window.location.hash === "#You page name")
{
navigator.app.exitApp();
}
else
{
window.history.back();
}


}

关于javascript - 如何使用 cordova 处理混合移动应用程序的后退按钮功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38177850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com