index.js 1.6 KB
Newer Older
1
import React, {Component} from 'react';
YYYTDMS's avatar
YYYTDMS 已提交
2
import {View, Text, StatusBar, StyleSheet, Platform, BackHandler, ToastAndroid} from 'react-native';
3 4 5
import MyHeader from '../../component/Header';
import Navigation from '../../component/Navigation';
class Home extends Component {
YYYTDMS's avatar
YYYTDMS 已提交
6 7 8 9
  constructor(props) {
    super(props);
    this.state = {name: '地图'};
    this.changeName = this.changeName.bind(this);
YYYTDMS's avatar
YYYTDMS 已提交
10
    this.onBackAndroid=this.onBackAndroid.bind(this);
YYYTDMS's avatar
YYYTDMS 已提交
11
  }
YYYTDMS's avatar
YYYTDMS 已提交
12

YYYTDMS's avatar
YYYTDMS 已提交
13
  UNSAFE_componentWillMount() {
YYYTDMS's avatar
YYYTDMS 已提交
14

YYYTDMS's avatar
YYYTDMS 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27
    if (Platform.OS === 'android') {
      BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
    }
  }

  componentWillUnmount() {
    if (Platform.OS === 'android') {
      BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
    }
  }


  onBackAndroid = () => {
YYYTDMS's avatar
YYYTDMS 已提交
28

YYYTDMS's avatar
YYYTDMS 已提交
29 30 31 32 33 34 35 36 37
    //禁用返回键
    if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
      BackHandler.exitApp(); //直接退出APP
    } else {
      this.lastBackPressed = Date.now();
      ToastAndroid.show('再按一次退出应用', 1000); //提示
      return true;
    }
  };
YYYTDMS's avatar
YYYTDMS 已提交
38

YYYTDMS's avatar
YYYTDMS 已提交
39 40 41 42
  changeName(name) {
    this.setState({
      name: name,
    });
43 44 45 46 47
  }

  render() {
    return (
      <View style={styles.container}>
YYYTDMS's avatar
YYYTDMS 已提交
48
        <MyHeader title={this.state.name} />
lilouv's avatar
改bug  
lilouv 已提交
49 50 51 52
        <Navigation
          navigation={this.props.navigation}
          changeName={this.changeName}
        />
53 54 55 56 57 58 59
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    backgroundColor: '#F2E2CE',
lilouv's avatar
改bug  
lilouv 已提交
60
    // opacity: 0.5,
61 62 63 64 65 66 67 68 69 70
    flex: 1,
  },
  text: {
    fontSize: 50,
    fontWeight: 'bold',
    textAlign: 'center',
  },
});

export default Home;