我可以成功地将postMessage发送到WebView,但我的问题是如何从event.data中提取json对象.是否有任何可能的方法来获取json对象
document.addEventListener("message", function(event) {
console.log("Received post message", event);//Get Event from React Native
alert(event.data);
}, false);
Run Code Online (Sandbox Code Playgroud)
以下是代码:
import React, { Component } from 'react'
import { ScrollView, Button, WebView, View, TouchableHighlight, Text } from 'react-native'
import { connect } from 'react-redux'
class UserData extends Component {
constructor (props) {
super(props)
this.state = {}
}
render () {
let html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" charset="utf-8"></script>
</head>
<body>
<div id="myContent">
</div>
</body>
</html> …Run Code Online (Sandbox Code Playgroud) 我已经定义了代码,专注于下一个TextInput字段
<View>
<TextInput
ref="1"
maxLength = {1}
keyboardType={"numeric"}
onChangeText={(pC1) => this.focusNextField('2',pC1)}
value={this.state.pC1}
/>
<TextInput
ref="2"
maxLength = {1}
keyboardType={"numeric"}
onChangeText={(pC2) => this.focusNextField('3',pC2)}
value={this.state.pC2}
/>
<TextInput
ref="3"
maxLength = {1}
keyboardType={"numeric"}
onChangeText={(pC3) => this.focusNextField('',pC3)}
value={this.state.pC3}
/>
</View>
Run Code Online (Sandbox Code Playgroud)
以下是为正向传输文本输入光标而编写的函数.但是如何以相反的顺序进行实现.也就是说,按退格键(键盘)
focusNextField(nextField,pinCode) {
if(nextField=="2"){
if(pinCode!=''){
this.state.pC1=pinCode; // Set value for PC1
this.refs[nextField].focus();//Goes to TextInput whose ref == 2
}else{
this.state.pC1='';
nextField="1";
this.refs[nextField].focus();
}
}else if(nextField=="3"){
if(pinCode!=''){
this.state.pC2=pinCode; // Set value for PC2
this.refs[nextField].focus();//Goes to TextInput whose ref == 3
}else{
this.state.pC2='';
nextField="2"; …Run Code Online (Sandbox Code Playgroud)