1、需求描述
因为要控制在返回上一页面的时候,如果有键盘,需要先隐藏键盘,所以需要自定义android手机的物理返回键单击事件,自己定义返回按钮的优先级操作,然后实现在首页面和登录页双击退出应用的操作。
2、准备
添加插件$cordovaToast,$cordovaKeyboard
cordova plugin add https://github.com/driftyco/ionic-plugins-keyboard.git cordova plugin add https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git
3、代码实现
引入$cordovaToast,$cordovaKeyboard的依赖。
angular.module(‘starter‘, [‘ionic‘, ‘route‘, ‘config‘, ‘global‘, ‘commonJs‘, ‘ngCordova‘]) .run([‘$ionicPlatform‘, ‘$rootScope‘, ‘$location‘, ‘$timeout‘, ‘$ionicHistory‘, ‘CommonJs‘, ‘$cordovaToast‘,‘$cordovaKeyboard‘, function ($ionicPlatform, $rootScope, $location, $timeout, $ionicHistory,CommonJs,$cordovaToast,$cordovaKeyboard) { $ionicPlatform.ready(function () { // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); } if (window.StatusBar) { // org.apache.cordova.statusbar required StatusBar.styleLightContent(); } }); //物理返回按钮控制&双击退出应用 $ionicPlatform.registerBackButtonAction(function (e) { //判断处于哪个页面时双击退出 if ($location.path() == ‘/tab/todoList‘ || $location.path() == ‘/tab/doneList‘ || $location.path() == ‘/tab/account‘|| $location.path() == ‘/login‘) { if ($rootScope.backButtonPressedOnceToExit) { ionic.Platform.exitApp(); } else { $rootScope.backButtonPressedOnceToExit = true; $cordovaToast.showShortBottom(‘再按一次退出系统‘); setTimeout(function () { $rootScope.backButtonPressedOnceToExit = false; }, 2000); } }else if ($ionicHistory.backView()) { if ($cordovaKeyboard.isVisible()) { $cordovaKeyboard.close(); } else { $ionicHistory.goBack(); } } else { $rootScope.backButtonPressedOnceToExit = true; $cordovaToast.showShortBottom(‘再按一次退出系统‘); setTimeout(function () { $rootScope.backButtonPressedOnceToExit = false; }, 2000); } e.preventDefault(); return false; }, 101); }]);
时间: 2024-10-09 05:19:09