一、简介
一个用于文本输入的基本组件。内置了多种特性,比如自动完成,自动大小写,以及多种不同的键盘类型。
二、TextInput
从TextInput里取值使用onChangeText事件这就是目前唯一的做法。
import React, { Component } from ‘react‘;
import { AppRegistry, TextInput } from ‘react-native‘;
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}
/>
);
}
}
// App registration and rendering
AppRegistry.registerComponent(‘AwesomeProject‘, () => UselessTextInput);
注意有些属性仅在multiline为true或者为false的时候有效。此外,当multiline=false时,为元素的某一个边添加边框样式(例如:borderBottomColor,borderLeftWidth等)将不会生效。为了能够实现效果你可以使用一个View来包裹TextInput:
import React, { Component } from ‘react‘;
import { AppRegistry, View, TextInput } from ‘react-native‘;
class UselessTextInput extends Component {
render() {
return (
<TextInput
{...this.props} // 将父组件传递来的所有props传递给TextInput;比如下面的multiline和numberOfLines
editable = {true}
maxLength = {40}
/>
);
}
}
class UselessTextInputMultiline extends Component {
constructor(props) {
super(props);
this.state = {
text: ‘Useless Multiline Placeholder‘,
};
}
// 你可以试着输入一种颜色,比如red,那么这个red就会作用到View的背景色样式上
render() {
return (
<View style={{
backgroundColor: this.state.text,
borderBottomColor: ‘#000000‘,
borderBottomWidth: 1 }}
>
<UselessTextInput
multiline = {true}
numberOfLines = {4}
onChangeText={(text) => this.setState({text})}
value={this.state.text}
/>
</View>
);
}
}
// App registration and rendering
AppRegistry.registerComponent(
‘AwesomeProject‘,
() => UselessTextInputMultiline
);
三、TextInput的API
四、方法
isFocused(): Boolean 返回值表明当前输入框是否聚焦
clear() 清空输入框的内容
原文:http://www.cnblogs.com/douglasvegas/p/6963383.html