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. type Props = {};
  25. var width = Dimensions.get('window').width;
  26. var height = Dimensions.get('window').height;
  27. export default class BasePage extends Component<Props> {
  28. render() {
  29. return (
  30. <ImageBackground
  31. style={{
  32. flex: 1,
  33. width: '100%',
  34. height: '100%',
  35. Background: '#F8F8F8'
  36. }}
  37. />
  38. );
  39. }
  40. getWindowHeight() {
  41. return height;
  42. }
  43. getWindowWidth() {
  44. return width;
  45. }
  46. clearPageToNext = (name, obj) => {
  47. const resetAction = StackActions.reset({
  48. index: 0,
  49. actions: [
  50. NavigationActions.navigate({ routeName: name }) //要跳转到的页面名字
  51. ]
  52. });
  53. this.props.navigation.dispatch(resetAction, obj);
  54. };
  55. toNextPage = (params, obj) => {
  56. //跳转之前移除当前界面的监听
  57. this.removeListener();
  58. this.props.navigation.navigate(params, obj);
  59. };
  60. Toast(params) {
  61. AndroidUtil.showToast(params, AndroidUtil.SHORT);
  62. }
  63. toWebPage(json) {
  64. AndroidUtil.toWebActivity(json);
  65. }
  66. testJssss() {
  67. //测试调用安卓方法然后安卓方法调用JS下面的监听
  68. AndroidUtil.testJS();
  69. }
  70. goBack() {
  71. //返回上一页
  72. this.props.navigation.goBack();
  73. }
  74. removeListener() {
  75. if (this.testJSaaa) {
  76. this.testJSaaa.remove();
  77. }
  78. if (this.toJsByAndroid) {
  79. this.toJsByAndroid.remove();
  80. }
  81. }
  82. componentWillMount() {
  83. //监听事件名为EventName的事件
  84. this.testJSaaa = DeviceEventEmitter.addListener('testJSaaa', (e) => {
  85. //e是原生传过来的参数,ceshi是安卓里面put的key
  86. alert(e.ceshi);
  87. });
  88. this.toJsByAndroid = DeviceEventEmitter.addListener('toJsByAndroid', (e) => {
  89. alert(e.name);
  90. alert(e.sex);
  91. alert(e.age);
  92. });
  93. }
  94. componentWillUnmount() {
  95. // 移除监听
  96. this.removeListener();
  97. }
  98. }
  99. const styles = StyleSheet.create({
  100. title_text: {
  101. justifyContent: 'center',
  102. alignItems: 'center',
  103. color: 'red',
  104. fontSize: 30,
  105. textAlign: 'center',
  106. marginTop: 30,
  107. marginBottom: 50
  108. }
  109. });