Ionic實現自定義返回按鍵事件

在 Android 和 Window 平臺下,有時候咱們須要監聽返回按鍵的事件來進行相應的操做,也就是自定義返回按鍵事件。下面根據一個例子來說解如何在 ionic 中自定義返回按鍵事件。javascript

功能需求

首先看下要經過自定義返回按鍵事件來實現的需求(均在點擊返回鍵操做以後):html

  • 若是鍵盤打開,則隱藏鍵盤;
  • 若是顯示 ionic 的 Loading 組件(在請求數據的時候),則隱藏 Loading 組件;
  • 若是當前頁面是子頁面,則返回上個頁面;若是是根頁面,則提示用戶是否退出應用。

具體實現

首先說明下,本示例的 ionic 應用的結構是 tabs 結構。java

  1. tabs.html 中的 ion-tabs 定義別名,如 mainTabs
<ion-tabs #mainTabs selectedIndex="1">
    <ion-tab [root]="tab1Root" tabTitle="頁面1"></ion-tab>
    <ion-tab [root]="tab2Root" tabTitle="頁面2"></ion-tab>
</ion-tabs>

ion-tabs 定義別名的目的是方便經過代碼找到 ion-tabs 的實例。app

  1. app.component.ts 中進行實現代碼編寫(實現細節在註釋中):
import { Component, ViewChild } from '@angular/core';
import { Nav, Platform, Keyboard, IonicApp, NavController, Tabs, ToastController, Toast } from 'ionic-angular';

@Component({
    templateUrl: 'app.html'
})
export class AppComponent {
    @ViewChild(Nav) nav: Nav; // ion-nav 引用
    backButtonPressed: boolean = false;  // 用於判斷返回鍵是否觸發

    constructor(public platform: Platform,
                public ionicApp: IonicApp,
                public toastCtrl: ToastController,
                public keyboard: Keyboard) {

        // 等待平臺加載完成以後註冊返回按鍵事件
        this.platform.ready().then(() => {
            this.registerBackButtonAction(); // 註冊返回按鍵事件
        });
    }

    /**
     * 註冊返回按鍵事件
     */
    registerBackButtonAction() {

        // 使用 registerBackButtonAction 方法進行自定義事件處理程序
        this.platform.registerBackButtonAction(() => {

            // 若是鍵盤開啓則隱藏鍵盤。實現要點:使用 ionic-plugin-keyboard 插件進行鍵盤的控制
            if (this.keyboard.isOpen()) {
                this.keyboard.close();
                return;
            }

            // 隱藏加載動畫。實現要點:經過 this.ionicApp._loadingPortal 獲取到表明 Loading 的 OverlayPortal,而後獲取當前被激活的 ViewController
            const activePortal: any = this.ionicApp._loadingPortal.getActive();
            if (activePortal) { // 若是有被激活的 ViewController 則將其隱藏。
                activePortal.dismiss();
                activePortal.onDidDismiss();
                return;
            }

            // 根據當前導航進行不一樣的處理( mainTabs 對象是在 TabsPage 定義的 ion-tabs )
            // 經過 this.nav.getActive().instance.mainTabs 獲取到別名爲 mainTabs 的 ion-tabs
            const mainTabs: Tabs = this.nav.getActive().instance.mainTabs;
            if (mainTabs) {

                // 獲取到當前被選中的 ion-tab
                const mainNav: NavController = mainTabs.getSelected();

                // 若是 ion-tab 能返回則返回上一個頁面,不能就直接退出應用
                mainNav.canGoBack() ? mainNav.pop() : this.showExit();
            } else {

                // 若是 ion-nav 能返回則返回上一個頁面,不能就直接退出應用
                this.nav.canGoBack() ? this.nav.pop() : this.showExit();
            }
            return;
        }, 1);
    }

    /**
     * 雙擊退出提示框
     */
    showExit() {
        if (this.backButtonPressed) { // 當觸發標誌爲 true 時,即2秒內雙擊返回按鍵則退出APP
            this.platform.exitApp();
        } else {
            const currentToast: Toast = this.toastCtrl.create({
                message: '再按一次退出應用',
                duration: 2000,
                position: 'top'
            });
            currentToast.present().then(() => {
                this.backButtonPressed = true;

                // 2秒內沒有再次點擊返回則將觸發標誌標記爲false
                const id: any = setTimeout(() => {
                    clearTimeout(id);
                    this.backButtonPressed = false;
                }, 2000);
            });
        }
    }
}
相關文章
相關標籤/搜索