0

每当我单击物理设备上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.
    }
});
4

2 回答 2

0

你需要传递valueonTextChange道具到TextInput.

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

于 2017-06-22T17:30:30.430 回答
0

您需要给它一个连接到状态的值,并且随着文本输入的变化,您更新状态值:

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>
    );
  }
}
于 2017-06-22T17:28:22.127 回答