BasePage.js 2.3 KB

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