BasePage.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. *
  5. * @format
  6. * @flow
  7. */
  8. import React, { Component } from 'react';
  9. import {
  10. Platform,
  11. StyleSheet,
  12. Text,
  13. View,
  14. Image,
  15. TouchableOpacity,
  16. ImageBackground,
  17. Button,
  18. Dimensions,
  19. DeviceEventEmitter
  20. } from 'react-native';
  21. import AndroidUtil from '../util/AndroidUtil';
  22. import { NavigationActions, StackActions } from 'react-navigation';
  23. import commonUtil from '../pages/utils/commonutil';
  24. import Toast from './components/Toast';
  25. type Props = {};
  26. var width = Dimensions.get('window').width;
  27. var height = Dimensions.get('window').height;
  28. export default class BasePage extends Component<Props> {
  29. render() {
  30. return (
  31. <ImageBackground
  32. style={{
  33. flex: 1,
  34. width: '100%',
  35. height: '100%',
  36. Background: '#F8F8F8'
  37. }}
  38. />
  39. );
  40. }
  41. getWindowHeight() {
  42. return height;
  43. }
  44. getWindowWidth() {
  45. return width;
  46. }
  47. clearPageToNext = (name, obj) => {
  48. const resetAction = StackActions.reset({
  49. index: 0,
  50. actions: [
  51. NavigationActions.navigate({ routeName: name }) //要跳转到的页面名字
  52. ]
  53. });
  54. this.props.navigation.dispatch(resetAction, obj);
  55. };
  56. toNextPage = (params, obj) => {
  57. //跳转之前移除当前界面的监听
  58. this.removeListener();
  59. this.props.navigation.navigate(params, obj);
  60. };
  61. Toast(message) {
  62. Toast.add(message);
  63. }
  64. toWebPage(json) {
  65. AndroidUtil.toWebActivity(json);
  66. }
  67. testJssss() {
  68. //测试调用安卓方法然后安卓方法调用JS下面的监听
  69. AndroidUtil.testJS();
  70. }
  71. goBack() {
  72. //返回上一页
  73. this.props.navigation.goBack();
  74. }
  75. removeListener() {
  76. if (this.testJSaaa) {
  77. this.testJSaaa.remove();
  78. }
  79. if (this.toJsByAndroid) {
  80. this.toJsByAndroid.remove();
  81. }
  82. }
  83. componentWillMount() {
  84. //监听事件名为EventName的事件
  85. this.testJSaaa = DeviceEventEmitter.addListener('testJSaaa', (e) => {
  86. //e是原生传过来的参数,ceshi是安卓里面put的key
  87. alert(e.ceshi);
  88. });
  89. this.toJsByAndroid = DeviceEventEmitter.addListener('toJsByAndroid', (e) => {
  90. alert(e.name);
  91. alert(e.sex);
  92. alert(e.age);
  93. });
  94. }
  95. componentWillUnmount() {
  96. // 移除监听
  97. this.removeListener();
  98. }
  99. }
  100. const styles = StyleSheet.create({
  101. title_text: {
  102. justifyContent: 'center',
  103. alignItems: 'center',
  104. color: 'red',
  105. fontSize: 30,
  106. textAlign: 'center',
  107. marginTop: 30,
  108. marginBottom: 50
  109. }
  110. });