国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Ionic2雙擊退出應用

2019-11-09 19:01:40
字體:
來源:轉載
供稿:網友

在Ionic2項目結構解析中,我們知道在 src/app/app.component.ts 初始化項目,因此我們可以在這里監聽Android返回按鍵,實現雙擊退出應用。

Ionic2 Platform中提供了監聽返回按鍵的API registerBackButtonAction(callback, PRiority),

參數 類型 描述
callback Function 返回鍵觸發時執行的方法
priority number 優先級,優先級高的會先執行,默認為0

注冊返回按鍵事件

修改src/app/app.component.ts如下:

import { Component, ViewChild } from '@angular/core';import { Platform, ToastController, Nav } from 'ionic-angular';import { StatusBar, Splashscreen } from 'ionic-native';import { TabsPage } from '../pages/tabs/tabs';@Component({ templateUrl: 'app.html'})export class MyApp { rootPage = TabsPage; backButtonPressed: boolean = false; //用于判斷返回鍵是否觸發 @ViewChild(Nav) nav: Nav; constructor(public platform: Platform, public toastCtrl: ToastController) { this.initializeApp(); } initializeApp() { this.platform.ready().then(() => { StatusBar.styleDefault(); Splashscreen.hide(); //注冊返回按鍵事件 this.platform.registerBackButtonAction((): any => { let activeVC = this.nav.getActive(); let page = activeVC.instance; if (!(page instanceof TabsPage)) { if (!this.nav.canGoBack()) { //當前頁面為tabs,退出APP return this.showExit(); } //當前頁面為tabs的子頁面,正常返回 return this.nav.pop(); } let tabs = page.tabs; let activeNav = tabs.getSelected(); if (!activeNav.canGoBack()) { //當前頁面為tab欄,退出APP return this.showExit(); } //當前頁面為tab欄的子頁面,正常返回 return activeNav.pop(); }, 101); }); } //雙擊退出提示框,這里使用Ionic2的ToastController showExit() { if (this.backButtonPressed) this.platform.exitApp(); //當觸發標志為true時,即2秒內雙擊返回按鍵則退出APP else { let toast = this.toastCtrl.create({ message: '再按一次退出應用', duration: 2000, position: 'bottom' }); toast.present(); this.backButtonPressed = true; //2秒內沒有再次點擊返回則將觸發標志標記為false setTimeout(() => { this.backButtonPressed = false; }, 2000) } }}

修改 src/pages/tabs/tabs.html

<ion-tabs #mainTabs> <ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="home"></ion-tab> <ion-tab [root]="tab2Root" tabTitle="About" tabIcon="information-circle"></ion-tab> <ion-tab [root]="tab3Root" tabTitle="Contact" tabIcon="contacts"></ion-tab></ion-tabs>

修改 src/pages/tabs/tabs.ts

import { Component,ViewChild } from '@angular/core';import { Tabs } from 'ionic-angular';import { HomePage } from '../home/home';import { AboutPage } from '../about/about';import { ContactPage } from '../contact/contact';@Component({ templateUrl: 'tabs.html'})export class TabsPage { @ViewChild('mainTabs') tabs:Tabs; //根據html中的'#mainTabs'獲取Tabs實例 tab1Root: any = HomePage; tab2Root: any = AboutPage; tab3Root: any = ContactPage; constructor() { }}

轉自https://dpary.github.io/2016/12/19/


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 综艺| 旺苍县| 信阳市| 衡山县| 阿克| 海阳市| 合江县| 武汉市| 宾阳县| 浙江省| 马边| 土默特右旗| 宁津县| 城固县| 安陆市| 原平市| 铜山县| 德令哈市| 吴川市| 邓州市| 桦南县| 兰考县| 称多县| 长子县| 江达县| 迁安市| 台前县| 孟村| 云阳县| 阿瓦提县| 惠州市| 应城市| 谷城县| 安乡县| 荆州市| 洛川县| 灵台县| 巫山县| 措勤县| 绥化市| 襄樊市|