React native (5) 处理文本输入

在上面的例子里,我们把text保存到state中,因为它会随着时间变化。

文本输入方面还有很多其他的东西要处理。比如你可能想要在用户输入的时候进行验证,在React的表单组件中的受限组件一节中有一些详细的示例(注意react中的onChange对应的是rn中的onChangeText)。此外你还需要看看TextInput的文档。

    constructor(props){
        super(props);

        //定义一个 myText 变量,用来存放输入框输入的值
        this.state={myText : ''}
    }

    render(){
        return (
          <View style={{padding:10, flex:1}}>
              <TextInput
                  style={{width:200,height:100}}
                placeholder="please input data"

                //在输入变动的时候,将最新的输入内容存到state里面去
                onChangeText={(myText) => this.setState({myText})}>

              </TextInput>

              //将值转换后输出
              <Text>{this.state.myText.split(' ').map((word) => word && '��').join(' ')}</Text>
          </View>
        );
    }

猜你喜欢

转载自blog.csdn.net/sinstar1/article/details/69944890