React-native之TextInput(7)

2017-01-29  本文已影响813人  飞奔的小马
一. 简介

TextInput是一个允许用户在应用中通过键盘输入文本的基本组件。本组件的属性提供了多种特性的配置,譬如自动完成、自动大小写、占位文字,以及多种不同的键盘类型(如纯数字键盘)等等。

二. 属性
三. 方法
四. 示例 文本框自动提示的搜索框

var onePT = 1 / PixelRatio.get();
class InputTextDemo extends Component {
render() {
return (
<View style={[styles.flex,styles.marginTop]}>
<Search></Search>
</View>
}
}
class Search extends Component {
constructor(props) {
super(props);
this.state = {
show: false,//是否显示匹配条目 value: null,//选中的值 }; }
hide(val) { this.setState({ show: false, value: val, }); }
getValue(text) { this.setState({ show: true, value: text, }); }
render() { return ( <View style={styles.flex}> <View style={styles.flexDirection}> <View style={[styles.flex,styles.input]}> <TextInput returnKeyType="search"//软键盘返回键 placeholder="请输入文字"//占位符 keyboardType="web-search" onChangeText={this.getValue.bind(this)}//监听输入字的改变 value={this.state.value}//匹配条目点击后选中的值 underlineColorAndroid="transparent" /> </View> <View style={styles.btn}> <Text style={styles.search} onPress={this.hide.bind(this,this.state.value)}>搜索</Text> </View> </View> {this.state.show? <View style={styles.result}> <Text onPress={this.hide.bind(this, this.state.value+"的习惯")} style={styles.item} numberOfLines={1}>{this.state.value}的习惯 </Text> <Text onPress={this.hide.bind(this, this.state.value+"的视频")} style={styles.item} numberOfLines={1}>{this.state.value}的视频</Text> <Text onPress={this.hide.bind(this, "呵呵"+this.state.value+"的文字")} style={styles.item} numberOfLines={1}>呵呵{this.state.value}的文字</Text> <Text onPress={this.hide.bind(this, this.state.value+"的图片")} style={styles.item} numberOfLines={1}>{this.state.value}的图片</Text> </View> :null } </View> ); } }

const styles = StyleSheet.create({ flex: { flex: 1 }, marginTop: { marginTop: 25, }, flexDirection: {//方向 因为RN默认是竖直方向 flexDirection: 'row', }, input: { borderWidth: 1, borderColor: 'red', height: 40, borderRadius: 5, paddingLeft: 10, marginLeft: 10, }, btn: { backgroundColor: '#23BEFF', height: 40, marginLeft: -5, marginRight: 5, width: 45, justifyContent: 'center', alignItems: 'center', }, search: { fontSize: 20, color: '#fff', fontWeight: 'bold', }, result: { marginTop: onePT, marginLeft: 18, marginRight: 10, height: 200, }, item: { fontSize: 16, paddingTop: 5, paddingBottom: 5, }, });

效果

TextInput_01.png

输入文本

TextInput_02.png

点击第一个条目

TextInput_03.png

记录我自己的RN学习之路,纯属自己增值,有什么不对的地方,一起讨论进步

上一篇 下一篇

猜你喜欢

热点阅读