javascript - TextInput onChangeText 无法正常工作 React Native

标签 javascript android react-native

我是这个平台的新手,这是我在这个平台上的第二天。我试图通过创建简单的登录、注册、新闻提要应用程序来自学它。我能够构建登录页面,但是在从 InputText 获取值时,它显示错误消息“不能同时指定值和子项”,我在这里发布我的 login.js 类

export default class LoginScreen extends Component {

    constructor(props) {
        super(props);
        this.state = {
            username: '',
            password: ''
        };
    }
    login = () => {
        alert(this.state.username + " " + this.state.password);
    }
    render() {
        return (
            <View style={styles.container}>

                <Image source={require('../images/hulk.jpg')} style={styles.backgroundimage}>
                    <View style={styles.content}>
                        <Image source={require('../images/logo.png')} style={styles.logo}>
                        </Image>

                        <View style={styles.inputcontainer}>

                            <TextInput underLineColorAndroid='transparent' style={styles.input} value={this.state.username} onChangeText={(username) => this.setState({ username })} placeholderText='username' > </TextInput>

                            <TextInput secureTextEntry={true} underLineColorAndroid='transparent' style={styles.input} value={this.state.password} onChangeText={(password) => this.setState({ password })} placeholderText='password' placeholderTextColor='black'> </TextInput>

                        </View>
                        <TouchableOpacity onPress={this.login} style={styles.buttonContainer}>
                            <Text style={styles.buttonText}>LOGIN</Text>
                        </TouchableOpacity>
                    </View>
                </Image>
            </View>
        );
    }
}

请让我知道我的代码哪里出错了

问候

最佳答案

你能试试这个吗

export default class LoginScreen extends Component {

constructor(props) {
    super(props);
    this.state = {username: '',password: ''};
}
login = () => {
    alert(this.state.username + " " + this.state.password);
}
render() {
    return (
        <View style={styles.container}>
            <Image source={require('../images/hulk.jpg')} style={styles.backgroundimage}>
                <View style={styles.content}>
                    <Image source={require('../images/logo.png')} style={styles.logo}>
                    </Image>
                    <View style={styles.inputcontainer}>
                        <TextInput 
                        underLineColorAndroid='transparent' 
                        style={styles.input} 
                        value={this.state.username} 
                        onChangeText={(text) => this.setState({ username: text })} 
                        placeholderText='username'/>

                        <TextInput 
                        secureTextEntry={true} 
                        underLineColorAndroid='transparent' 
                        style={styles.input} 
                        value={this.state.password} 
                        onChangeText={(text) => this.setState({ password:text })} 
                        placeholderText='password' 
                        placeholderTextColor='black'/>
                    </View>
                    <TouchableOpacity onPress={this.login()} style={styles.buttonContainer}>
                        <Text style={styles.buttonText}>LOGIN</Text>
                    </TouchableOpacity>
                </View>
            </Image>
        </View>
    );
}

关于javascript - TextInput onChangeText 无法正常工作 React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44064308/

相关文章:

java - 增强现实 QualComms Vuforia

javascript - 如何设置 redux-sagas 和文件结构?

react-native - React Native - 在导航到/从包含嵌入在其自己的 WebView 中的多条推文的页面导航后取消引用空指针

javascript - 如何使用xul在firefox中添加的javascript中更改工具栏按钮的背景颜色

Javascript - 如果父类(super class)构造函数采用参数,如何初始化父类(super class)

Java静态内部类返回null

android - 如何在 react-native 中获取上下文

javascript - 缩小原型(prototype)会引入错误?

javascript - 选择选择选项toggleClass时不强制关闭怎么办

android - @BidingMethod 不适用于方法