react-native中的TextInput
TextInput是一个允许用户输入文本的基础组件。它有一个名为onChangeText的属性,此属性接受一个函数, import React,{ Component } from 'react'; import { AppRegistry,Text,TextInput,View } from 'react-native'; export default class PizzaTranslator extends Component { constructor(props) { super(props); this.state = {text: ''}; } render() { return ( <View style={{padding: 10}}> <TextInput style={{height: 40}} placeholder="Type here to translate!" onChangeText={(text) => this.setState({text})} /> <Text style={{padding: 10,fontSize: 42}}> {this.state.text.split(' ').map((word) => word && '??????????').join(' ')} </Text> </View> ); } } 在上面的例子里,我们把text保存到 state 中,因为它会随着时间变化。 import React,{ Component } from 'react'; import { TextInput } from 'react-native'; export default class UselessTextInput extends Component { constructor(props){ super(props); this.state = { text:'请输入任意字符'} } render() { return ( <TextInput style={{height: 40,borderColor:'gray',borderWidth: 1}} onChangeText={(text) => this.setState({text})} value={this.state.text} /> ) } } 注意有些属性仅在multiline为true或者为false的时候有效。此外,当multiline=false时, import React,{ Component } from 'react'; import { TextInput } from 'react-native'; export default class UselessTextInput extends Component { constructor(props) { super(props); this.state = { text: 'Useless Placeholder' }; } render() { return ( <TextInput style={{height: 40,borderColor: 'gray',borderWidth: 1}} onChangeText={(text) => this.setState({text})} value={this.state.text} /> ); } } 下面的这些,窝还没有遇到过: 又又,在安卓上长按选择文本会导致windowSoftInputMode设置变为adjustResize,这样可能导致绝对定位的元素被键盘给顶起来。 更多属性,见下面链接:https://reactnative.cn/docs/textinput/#allowfontscaling (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |