標籤:
1、需求描述
因為要控制在返回上一頁面的時候,如果有鍵盤,需要先隱藏鍵盤,所以需要自訂android手機的物理返回鍵單擊事件,自己定義返回按鈕的優先順序操作,然後實現在首頁面和登入頁雙擊退出應用的操作。
2、準備
添加外掛程式$cordovaToast,$cordovaKeyboard
cordova plugin add https://github.com/driftyco/ionic-plugins-keyboard.gitcordova 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); }]);
Ionic系列——android雙擊退出應用和物理返回按鈕隱藏鍵盤的實現