BasePage.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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) => {
  45. //跳转之前移除当前界面的监听
  46. this.removeListener();
  47. console.log('跳转参数',params)
  48. console.log(this.props)
  49. this.props.navigation.navigate(params);
  50. }
  51. Toast(params) {
  52. AndroidUtil.showToast(params, AndroidUtil.SHORT);
  53. }
  54. toWebPage(json) {
  55. AndroidUtil.toWebActivity(json);
  56. }
  57. testJssss() {
  58. //测试调用安卓方法然后安卓方法调用JS下面的监听
  59. AndroidUtil.testJS();
  60. }
  61. goBack() {
  62. //返回上一页
  63. this.props.navigation.goBack();
  64. }
  65. removeListener() {
  66. if (this.testJSaaa) {
  67. this.testJSaaa.remove();
  68. }
  69. if (this.toJsByAndroid) {
  70. this.toJsByAndroid.remove();
  71. }
  72. }
  73. componentWillMount() {
  74. //监听事件名为EventName的事件
  75. this.testJSaaa = DeviceEventEmitter.addListener("testJSaaa", e => {
  76. //e是原生传过来的参数,ceshi是安卓里面put的key
  77. alert(e.ceshi);
  78. });
  79. this.toJsByAndroid = DeviceEventEmitter.addListener("toJsByAndroid", e => {
  80. alert(e.name);
  81. alert(e.sex);
  82. alert(e.age);
  83. });
  84. }
  85. componentWillUnmount() {
  86. // 移除监听
  87. this.removeListener();
  88. }
  89. }
  90. const styles = StyleSheet.create({
  91. title_text: {
  92. justifyContent: "center",
  93. alignItems: "center",
  94. color: "red",
  95. fontSize: 30,
  96. textAlign: "center",
  97. marginTop: 30,
  98. marginBottom: 50
  99. }
  100. });