android - 如何在不使用 react native android中的状态的情况下获取TextInput值

标签 android react-native textinput

我在这里尝试使用 const 值用户名和密码获取 TextInput 值,但未定义。如何在此使用状态或 Prop 。我遵循了本教程 https://medium.com/react-native-training/react-native-navigator-experimental-part-2-implementing-redux-c6acbf66eca1#.d0vteepp7

import React, { Component } from 'react'
import {
    View,
    Text,
    TextInput,
    Alert
} from 'react-native'
import Button from './Button'

     const route = {
        type: 'push',
        route: {
            key: 'about',
            title: 'About'
        }
    }

    const _values = {
        username: '',
        password: ''
    }

    const LoginView = ({_handleNavigate}) => {

        const _handlePress = () => {

            const {username, password} = _values
            console.log('username' + username + "" + password);

            return fetch('http://webservice.net/User/ValidateUser', {
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    UserName: username,
                    Password: password,
                    DeviceType: 'Android',
                    DeviceToken: 'eU0z3IR96Mg:APA91bHNyEw3CcMCHZ-MmGhFVRV8XT292NYzrD2xedMFLXdaJqcJ4DXlBmn'
                })
            }).then(response => response.json())
                .then(response => {
                    console.log(response);
                    Alert.alert('Response message is:', response.parse)
                    _handleNavigate(route)
                    return response;
                })
                .catch(error => {
                    console.error(error);
                });
        };

        return (
            <View style={{ flex: 1, backgroundColor: 'steelblue', padding: 10, justifyContent: 'center' }}>
                <Text style={{ backgroundColor: 'steelblue', justifyContent: 'center', alignItems: 'center', fontSize: 40, textAlign: 'center', color: 'white', marginBottom: 30 }}>
                    LOGIN
                    </Text>
                <View style={{ justifyContent: 'center' }}>
                    <TextInput
                        style={{ height: 40, margin: 30, color: 'white', fontSize: 20 }}
                        placeholder='Username' placeholderTextColor='white'
                        autoFocus={true}
                        />
                    <TextInput
                        secureTextEntry={true}
                        style={{ height: 40, margin: 30, color: 'white', fontSize: 20 }}
                        placeholder='Password' placeholderTextColor='white'
                        />
                </View>
                <Button onPress={() => { _handlePress() } } label='Login' />
            </View>
        )
    }
export default LoginView

最佳答案

您可以附加 TextInputonChangeText事件'处理程序并存储其参数,例如:

<TextInput
   onChangeText={(val) => _values.username = val}
   ...
/>

关于android - 如何在不使用 react native android中的状态的情况下获取TextInput值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40788956/

相关文章:

react-native - 在 FlatList 上 react Native "keyboardDismissMode"

apache-flex - 当应用程序启动时有两种状态时将焦点设置在 TextField 上

android - 我的应用程序背景无法填满屏幕

java - android捕获的视频质量差,缓冲视频时出错

android canvas 删除之前绘制的路径

java - 致命异常 : AsyncTask #1 Relating to java. lang.NoClassDefFoundError

javascript - 将数据从 React Native 中的数组绑定(bind)到 TextInput

reactjs - 动态改变React Native平面列表中的列数

android - 找不到构建工具修订版 23.0.1

javascript - HTML5 : How do you place a fixed Text Input over a centering canvas?