一、简介

一个应用程序中,输入框基本不可或缺,它衍生的搜索功能在很多APP中随处可见。在iOS开发中,使用的输入框组件是UITextView和UITextField,在React-Native中使用的则是TextInput组件。TextInput组件可以通过键盘将文本输入到APP的组件,它提供了如自动校验、占位符、键盘样式、焦点函数等很多丰富的功能。

二、API

TextInput组件提供的属性和事件基本能够满足开发需求,既可以使用它做基本的输入功能,也能做列表补全的搜索功能。TextInput组件主要属性和事件如下:

属性或者函数 作用
autoCapitalize
枚举类型,用于信息提示,可选值包括none、sentences、words、characters
placeholder
占位符,在输入前显示文本内容
value
文本框输入的内容
placeholderTextColor
占位符文本的颜色
password
如果为true,显示为密码输入框,文本显示为"*"号
multiline
如果为true,则是多行输入
editable
如果为false,文本框不可以输入,默认为true

keyboardType

   枚举类型,表示键盘类型,可选值包括'default','email-address','numeric','phone-pad','ascii-capable','numbers-and-punctuation','url','number-pad','name-phone-pad','decimal-pad','twitter','web-search',
autoFocus
如果为true,将自动聚焦
clearButtonMode
枚举类型,用于显示清除按钮,可选值包括never、while-editing、unless-editing、always
maxLength
能够输入的最长字符数
enablesReturnKeyAutomatically
如果为true,表示无文本时键盘不显示返回键,默认为false
returnKeyType
枚举类型,表示键盘返回键显示的字符串,可选值包括default、go、google、join、next、route、search、send、yahoo、done、emergency-call
securityEntry
如果为true,则像密码框一样隐藏输入内容,默认为false
onchangeText
当文本的输入框内容发生改变时调用。它会接收一个文本的参数对象
onChange
当文本发生改变时,调用该函数
onEndEditing
当结束编辑时,调用该函数
onBlur
失去焦点触发事件
onFocus
获取焦点触发事件
onsubmitEditing
当结束编辑后,点击键盘的提交按钮时触发事件

三、使用

1、作为输入框

InputView.js

import React, { Component } from 'react';

import {
StyleSheet,
View,
Text,
TextInput
} from 'react-native' export default class InputView extends Component{ constructor(props){
super(props);
this.state = {content:"当前无内容"}
} render(){
return (
<View style={styles.flex}>
<View style={styles.content}>
<Text style={styles.text}>{this.state.content}</Text>
</View>
<View style={styles.flex}>
<TextInput
style={styles.input}
placeholder='请输入您想要的东西'
placeholderTextColor={"#CCC"}
returnKeyType='done'
clearButtonMode='while-editing'
enablesReturnKeyAutomatically={true}
editable={true}
maxLength={100}
keyboardType='default'
onFocus={()=> console.log("--获取焦点触发事件--")}
onBlur={()=> console.log("--失去焦点触发事件--")}
onChange={() => console.log("---当文本发生改变时,调用该函数--")}
onEndEditing={() => console.log("--当结束编辑时,调用该函数--")}
onSubmitEditing={ () => console.log("--当结束编辑后,点击键盘的提交按钮时触发事件--")}
onChangeText={(text) => this.setState({content:text})}
/>
</View>
</View>
)
} } const styles = StyleSheet.create({
flex:{
flex: 1
},
content:{
height: 200,
backgroundColor:"red",
justifyContent: 'center',
alignItems:'center'
},
input:{
height:45,
borderWidth: 1,
marginTop: 20,
marginRight:5,
marginLeft: 5,
paddingLeft: 5,
borderColor:'#CCC',
borderRadius: 4
},
text:{
fontSize:20,
color:'white',
textAlign:'center'
}
})

index.ios.js

/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/ import React, { Component } from 'react';
import InputView from './src/InputView' import {
AppRegistry,
View
} from 'react-native'; export default class ReactNativeDemo extends Component { render() {
return (
<View style={{flex: 1}}>
<InputView/>
</View>
);
}
} AppRegistry.registerComponent('ReactNativeDemo', () => ReactNativeDemo);

日志和gif如下:

-- ::43.022 [info][tid:com.facebook.react.JavaScript] --获取焦点触发事件--
-- ::48.992 [info][tid:com.facebook.react.JavaScript] ---当文本发生改变时,调用该函数--
-- ::49.951 [info][tid:com.facebook.react.JavaScript] ---当文本发生改变时,调用该函数--
-- ::51.016 [info][tid:com.facebook.react.JavaScript] ---当文本发生改变时,调用该函数--
-- ::52.893 [info][tid:com.facebook.react.JavaScript] ---当文本发生改变时,调用该函数--
-- ::55.243 [info][tid:com.facebook.react.JavaScript] --当结束编辑后,点击键盘的提交按钮时触发事件--
-- ::55.248 [info][tid:com.facebook.react.JavaScript] --当结束编辑时,调用该函数--
-- ::55.256 [info][tid:com.facebook.react.JavaScript] --失去焦点触发事件--

2、制作搜索框

InputSearchView.js

import React, { Component } from 'react';

import {
StyleSheet,
View,
Text,
TextInput,
PixelRatio
} from 'react-native' export default class InputSearchView extends Component{ constructor(){
super();
this.state = {show:false, keywords:""}
} show = () => {
this.setState({show:this.state.keywords.length>0})
}; render(){
return (
<View style={styles.flex}>
<View style={[styles.flexDirection,styles.height]}>
<View style={styles.flex}>
<TextInput
value={this.state.keywords}
style={styles.input}
placeholder='请输入关键字'
placeholderTextColor={"#CCC"}
returnKeyType='search'
clearButtonMode='while-editing'
enablesReturnKeyAutomatically={true}
onChangeText={ (text) => this.setState({keywords:text})}
onEndEditing={this.show.bind(this)}
/>
</View>
<View style={styles.btn}>
<Text style={styles.text} onPress={this.show.bind(this)}>
搜索
</Text>
</View>
</View>
{
this.state.show?
<View style={styles.list}>
<Text style={styles.list_item}>I AM XYQ</Text>
<Text style={styles.list_item}>T FROM CHINA</Text>
<Text style={styles.list_item}>I AM 28 YEARS OLD</Text>
<Text style={styles.list_item}>WELCOME TO ME</Text>
</View> : null
} </View>
)
}
} const styles = StyleSheet.create({
flex:{
flex: 1
},
flexDirection:{
flexDirection: 'row'
},
list:{
marginTop: 28,
height: 200,
marginLeft: 5,
marginRight: 5,
borderColor:"red",
borderTopWidth: 1/PixelRatio.get(),
},
list_item:{
fontSize:20,
fontWeight:'bold',
padding: 5,
paddingTop: 10,
paddingBottom: 10,
borderWidth: 1/PixelRatio.get(),
borderColor: 'red',
borderTopWidth: 0
},
height:{
height: 45
},
input:{
height:45,
borderWidth: 1,
fontSize:20,
marginTop: 25,
marginLeft: 5,
paddingLeft: 5,
borderColor:'#CCC',
borderRadius: 4
},
btn:{
width:60,
height:45,
borderWidth: 1,
marginTop: 25,
marginRight: 5,
paddingLeft: 5,
backgroundColor:'#23BEFF',
borderColor:'#CCC',
borderTopRightRadius:4,
borderBottomRightRadius:4,
justifyContent: 'center',
alignItems:'center'
},
text:{
fontSize:20,
color:'white',
fontWeight:'bold',
textAlign:'center'
}
})

index.ios.js

/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/ import React, { Component } from 'react';
import InputSearchView from './src/InputSearchView' import {
AppRegistry,
View
} from 'react-native'; export default class ReactNativeDemo extends Component { render() {
return (
<View style={{flex: 1}}>
<InputSearchView/>
</View>
);
}
} AppRegistry.registerComponent('ReactNativeDemo', () => ReactNativeDemo);

ReactNative: 使用输入框TextInput组件的更多相关文章

  1. react-native 学习之TextInput组件篇

    /** * Sample React Native App * https://github.com/facebook/react-native */ 'use strict'; import Rea ...

  2. React-Native之轮播组件looped-carousel的介绍与使用

    React-Native之轮播组件looped-carousel的介绍与使用 一,关于react-native轮播组件的介绍与对比 1,react-native-swiper在动态使用网页图片,多张图 ...

  3. TextInput组件的常用属性

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

  4. react native 封装TextInput组件

    上一篇 react-native文章提到了TextInput组件对安卓的适配问题,因此对该组件进行封装很有必要. 文章地址  react native定报预披项目知识点总结 TextInput介绍 官 ...

  5. JS 仿支付宝input文本输入框放大组件

    input输入的时候可以在后边显示数字放大镜 <!doctype html> <html lang="en"> <head> <meta ...

  6. [React Native]高度自增长的TextInput组件

    之前我们学习了从零学React Native之11 TextInput了解了TextInput相关的属性. 在开发中,我们有时候有这样的需求, 希望输入区域的高度随着输入内容的长度而增长, 如下: 这 ...

  7. react-native DatePicker日期选择组件的实现

    本教程的实现效果如下: 为了实现其淡入/淡出的覆盖效果, 还有取消按钮, 在此用了一个三方的组件, 大家可以先安装一下: 三方组件的地址:https://github.com/eyaleizenber ...

  8. react-native学习笔记——ViewStack组件

    今天来写一个组件,相信很多人都会用到的——ViewStack. ViewStack组件无疑是UI中很重要的一个组件,可惜react-native并没有内嵌进去,需要开发者自己去实现. 实现原理很简单, ...

  9. react-native获取设备信息组件(react-native-device-info)

    转载链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native-acquisition-device-infor ...

随机推荐

  1. 016_List/Set/Map

    先写一下3这种遍历方法 for循环 List<Teacher> list = new ArrayList<>(); list.add(new Teacher("张三& ...

  2. 大数据学习笔记——Java篇之集合框架(ArrayList)

    Java集合框架学习笔记 1. Java集合框架中各接口或子类的继承以及实现关系图: 2. 数组和集合类的区别整理: 数组: 1. 长度是固定的 2. 既可以存放基本数据类型又可以存放引用数据类型 3 ...

  3. Java设计模式的7种设计原则还有很多人不知道

    前言 其实没有设计模式我们也能完成开发工作.但是为什么需要设计模式呢?让你看起来很牛,没错这个算一个.让你的代码层次感分明,可读性强而且容易维护.让你像我一样有更多的摸鱼划水时间. 可能有人说我一个类 ...

  4. 3个Spring Boot核心注解,你知道几个?

    Spring Boot 核心注解讲解 Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置. 所以 S ...

  5. textarea增加字数监听且高度自适应(兼容IE8)

    1.封装方法: var textareaListener = { /*事件监听器兼容 * *attachEvent——兼容:IE7.IE8:不兼容firefox.chrome.IE9.IE10.IE1 ...

  6. 百度大脑UNIT3.0详解之知识图谱与对话

    如今,越来越多的企业想要在电商客服.法律顾问等领域做一套包含行业知识的智能对话系统,而行业或领域知识的积累.构建.抽取等工作对于企业来说是个不小的难题,百度大脑UNIT3.0推出「我的知识」版块专门为 ...

  7. mysql中msvcr120.dll文件丢失问题

    安装VC++2013 若是以上方法不能解决,需要下载安装VC++2013,这是微软官网的链接 https://www.microsoft.com/zh-cn/download/confirmation ...

  8. juc-2.1-模拟CAS算法

    package com.wf.zhang.juc; /* * 模拟 CAS 算法 */ public class TestCompareAndSwap { public static void mai ...

  9. OpenLDAP的docker版安装

    分为服务器和client的web版. ldap.sh #!/bin/bash -e docker run --name ldap-service -- docker run --name phplda ...

  10. android:Program type already present: android.support.v4.app.INotificationSideChannel

    背景 这个错误的原因是:androidx和 android.support同时存在. 首先,网上有很多解答说这样处理: 在 gradle.properties 添加: android.useAndro ...