ionic2开发(五)返回按钮处理

Ionic2开发app注册安卓硬件返回按钮事件是必须的,因为用户不小心点击了返回按钮就退出app体验很不好. 所以当用户点击返回按钮,应该提示"再按一次退出"。

借鉴了一下网上大神们的代码,结合自己的实际情况做了点修改,下面贴代码:

1、app.html:

添加#myNav,在app.component.ts文件通过@ViewChild('myNav')获取

<ion-nav #myNav [root]="rootPage"></ion-nav>

2、app.component.ts:

import { Component, ViewChild } from '@angular/core';
import { Platform, ToastController, Nav, IonicApp, Keyboard} from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { TabsPage } from '../pages/tabs/tabs';
@Component({
    templateUrl: 'app.html'
})
export class MyApp {
    public rootPage;
    backButtonPressed: boolean = false;  //用于判断返回键是否触发
    @ViewChild('myNav') nav: Nav;

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

        platform.ready().then(() => {
            // Okay, so the platform is ready and our plugins are available.
            // Here you can do any higher level native things you might need.
            StatusBar.styleDefault();
            Splashscreen.hide();
            this.registerBackButtonAction();//注册返回按键事件

            /*===========设置rootPage(此部分代码根据情况自己添加)===============*/
   
}

    //返回按键处理
registerBackButtonAction() {
        this.platform.registerBackButtonAction(() => {
if (this.keyBoard.isOpen()) {
                this.keyBoard.close();
            }
            else {
            let activeVC = this.nav.getActive();
            let page = activeVC.instance;
            //此处if是rootPage为登录页的情况,else是rootPage为TabsPage(如果不需要判断登录页的情况直接用else内的代码即可)
            if (!(page instanceof TabsPage)) {
                if (!this.nav.canGoBack()) {
                    console.log("检测到在根视图点击了返回按钮");
                    this.showExit();
                }
                else {
                    console.log("检测到在子路径中点击了返回按钮。");
                    this.nav.pop();
                }
            }
            else {
                let tabs = page.tabs;
                let activeNav = tabs.getSelected();
                if (!activeNav.canGoBack()) {
                    console.log('检测到在 tab 页面的顶层点击了返回按钮。');
                    this.showExit();
                }
                else {
                    console.log('检测到当前 tab 弹出层的情况下点击了返回按钮。');
                    activeNav.pop();
                }
}
            }
        }, 1);
    }

    //双击退出提示框
    showExit() {
        if (this.backButtonPressed) { //当触发标志为true时,即2秒内双击返回按键则退出APP
            this.platform.exitApp();
        } else {
            this.toastCtrl.create({
                message: "再按一次退出应用",
                duration: 2000,
                position: 'bottom'
                //cssClass: 'toastcss' //修改样式(根据需要添加)
            }).present();
            this.backButtonPressed = true;
            setTimeout(() => this.backButtonPressed = false, 2000);//2秒内没有再次点击返回则将触发标志标记为false
        }
 }
}

3、tabs.html:

添加#mainTabs,在tabs.ts文件通过@ViewChild('mainTabs') tabs:Tabs;获取
<ion-tabs  #mainTabs>
    <ion-tab [root]="tab1Root" tabTitle="页面1" tabIcon="ios-pulse"></ion-tab>
    <ion-tab [root]="tab2Root" tabTitle="页面2" tabIcon="ios-information-circle"></ion-tab>
    <ion-tab [root]="tab3Root" tabTitle="页面3" tabIcon="ios-analytics"></ion-tab>
    <ion-tab [root]="tab4Root" tabTitle="页面4" tabIcon="ios-construct"></ion-tab>
</ion-tabs>

4、tabs.ts:

import { Component,ViewChild} from '@angular/core';
import { Tabs } from "ionic-angular";

//(这里自己根据情况写)
import { Page1 } from '..;
import { Page2 } from '..';
import { Page3 } from '..';
import { Page4 } from '..';

@Component({
    templateUrl: 'tabs.html'
})
export class TabsPage {
    @ViewChild('mainTabs') tabs: Tabs;
    // this tells the tabs component which Pages
    // should be each tab's root Page
    tab1Root: any = Page1;
    tab2Root: any = Page2;
    tab3Root: any = Page3;
    tab4Root: any = Page4;

    constructor() {
    }
}

如果大家有更好的方法欢迎与我多多交流,本人学习中

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值