BasePage.js 2.1 KB

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