为什么不会<textinput>显示正在键入的内容?

时间:2017-06-22 17:20:31

标签: javascript ios reactjs react-native expo

每当我点击物理设备上第一个Email框中的<TextInput/>时,我就无法看到所输入的内容。只有当我按Next转到Password框时,才能看到Email框中输入的内容。为什么会这样呢?

此处App.js

import React, {Component} from 'react';
import {View, StyleSheet} from 'react-native';
import BackGround from './components/BackGround';

export default class App extends Component {
    render() {
        return(
            <View style={styles.back}>
                <BackGround/>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    back: {
        flex: 1
    }
});

此处Login.js

import React, {Component} from 'react';
import {StyleSheet, TextInput, View, Text, TouchableOpacity, KeyboardAvoidingView} from 'react-native';

class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {
            text: ''
        };
    }

    updateTextInput = text => this.setState({text});

    render() {
        return(
            <KeyboardAvoidingView behavior={"padding"} style={styles.container}>
                <View>
                    <TextInput
                        style={styles.input}
                        returnKeyType={"next"}
                        keyboardType={"email-address"}
                        autoCorrect={false}
                        onChangeText={this.updateTextInput}
                        value={this.state.text}
                    />

                    <TextInput
                        style={styles.input}
                        returnKeyType={"go"}
                        secureTextEntry
                    />

                    <TouchableOpacity>
                        <Text style={styles.loginAndCA}>Login</Text>
                    </TouchableOpacity>

                    <TouchableOpacity>
                        <Text style={styles.loginAndCA}>Create Account</Text>
                    </TouchableOpacity>
                </View>
            </KeyboardAvoidingView>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        padding: 20
    },

    input: {
        backgroundColor: '#DAE5FF',
        paddingBottom: 20,
        padding: 20,
        paddingHorizontal: 15,
        marginBottom: 10,
        borderRadius: 15,
    },

    loginAndCA: {
        fontSize: 40,
        textAlign: 'center',
        color: 'white',
        fontFamily: 'Bodoni 72 Smallcaps',
        paddingHorizontal: 10
    },

    buttons: {
        paddingBottom: 50
    }
});

export default Login;

此处BackGround.js

import React, {Component} from 'react';
import {StyleSheet, Image, View} from 'react-native';
import Login from './Login';

export default class BackGround extends Component {
    render() {
        return(
            <View style={styles.first}>
                <Image style={styles.container} source={require('../pictures/smoke.jpg')}>
                    <View style={styles.second}>
                        <Login/>
                    </View>
                </Image>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        width: null,
        height: null,
        backgroundColor: 'rgba(0,0,0,0)',
        resizeMode: 'cover'
    },

    first: {
        flex: 1
    },

    second: {
       paddingTop: 290 // Moves both <TextInput> boxes down.
    }
});

2 个答案:

答案 0 :(得分:0)

您需要为其指定一个与状态相关的值,并且当textinput更改时,您需要更新状态值:

export default class TextInputExample extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: ''
    };
  }
  updateTextInput = text => this.setState({ text })

  render() {
    return (
      <View style={{ flex: 1}}>
        <TextInput
          style={{height: 40}}
          placeholder="Type here!"
          onChangeText={this.updateTextInput}
          value={this.state.text}
        />
      </View>
    );
  }
}

答案 1 :(得分:0)

您需要将valueonTextChange道具传递给TextInput

https://facebook.github.io/react-native/docs/textinput.html