React native连续按两次退出app

这个博客展示了如何在React Native应用中实现一个底部导航栏,包括导入必要的库,定义屏幕选项以改变图标颜色,以及处理Android设备的返回键事件。代码示例详细解释了每个组件和方法的作用,如`createBottomTabNavigator`用于创建底部导航,`BackHandler`监听硬件返回键。
摘要由CSDN通过智能技术生成

代码(别忘记导入包):

import React, { Component } from 'react';

import {BackHandler, Platform, ToastAndroid} from 'react-native';
import { createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import {Alert, Image, LogBox} from 'react-native';


import HomeScreen from './home_page';
import WorkScreen from './work_page';
import FindScreen from './find_page';
import MineScreen from './mine_page';

const Tab = createBottomTabNavigator();

class MyTabs extends Component {

        render() {
            return (
                <Tab.Navigator
                    // 图标切换
                    screenOptions={({route}) => ({
                        tabBarIcon: ({ focused, color, size }) => {
                            let image;
                            if(route.name === 'Home') {
                                if (focused) {
                                    image = <Image source={require('../../img/select-home.png')} style={{width: 23, height: 23}}></Image>
                                } else {
                                    image = <Image source={require('../../img/gray-home.png')} style={{width: 23, height: 23}}></Image>
                                }
                            } else if(route.name === 'Work'){
                                if (focused) {
                                    image = <Image source={require('../../img/work-select.png')} style={{width: 23, height: 23}}></Image>
                                } else {
                                    image = <Image source={require('../../img/work-normal.png')} style={{width: 23, height: 23}}></Image>
                                }
                            } else if(route.name === 'Find'){
                                if (focused) {
                                    image = <Image source={require('../../img/resource-select.png')} style={{width: 23, height: 23}}></Image>
                                } else {
                                    image = <Image source={require('../../img/resource-normal.png')} style={{width: 23, height: 23}}></Image>
                                }
                            } else if(route.name === 'Mine'){
                                if (focused) {
                                    image = <Image source={require('../../img/mine-select.png')} style={{width: 23, height: 23}}></Image>
                                } else {
                                    image = <Image source={require('../../img/mine-normal.png')} style={{width: 23, height: 23}}></Image>
                                }
                            }
                            return image;
                        }
                    })}
    
                    // 底部文字切换颜色
                    tabBarOptions={{
                        activeTintColor: 'tomato',
                        inactiveTintColor: 'gray',
                    }}
                    
                >
                    <Tab.Screen name="Home" component={HomeScreen} />
                    <Tab.Screen name="Work" component={WorkScreen} />
                    <Tab.Screen name="Find" component={FindScreen} />
                    <Tab.Screen name="Mine" component={MineScreen} />
                </Tab.Navigator>
            );
        }

        componentDidMount() {
            if (Platform.OS === 'android'){
              BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
            }
          }
        
         componentWillUnmount() {
            if (Platform.OS === 'android') {
              BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
            }
          }
        
         onBackAndroid = () => {
            //禁用返回键
            if(this.props.navigation.isFocused()) {//判断   该页面是否处于聚焦状态
                if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
                  //最近2秒内按过back键,可以退出应用。
                  // return false;
                  BackHandler.exitApp();//直接退出APP
                }else{
                  this.lastBackPressed = Date.now();
                  ToastAndroid.show('再按一次退出应用', 1000);//提示
                  return true;
                }
            }
        }

}

export default module = MyTabs;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值