react native TextInput各个参数详解,内含事例动图

TextInput 文本输入框

  • React Native中的文本输入框使用和iOS比较相近,可能是因为 RN 首先封装iOS端的缘故(这点对iOS开发者来说是个好消息)
  • TextInput也是继承自 View,所以 View 的属性 TextInput 也能使用,一些样式类的属性可以参照 View 的相关属性

  • 为了更好的讲解 TextInput,先创建一个基本的文本输入框

    // 视图
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput style={styles.textInputStyle}></TextInput>
                </View>
            );
        }
    });

    // 样式
    var styles = StyleSheet.create({
        container: {
            flex:1
        },

        textInputStyle: {
            // 设置尺寸
            width:width,
            height:40,
            marginTop:100,
            // 设置背景颜色
            backgroundColor:'green'
        }
    });

效果:

初始化效果.gif

  • Value:文本输入的默认值(注:如果设置了此属性,会造成无法输入的尴尬,一般会搭配JS动态设置)
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        value="设置了Value"
                    ></TextInput>
                </View>
            );
        }
    });

效果:

设置了Value.gif

  • keyboardType:设置键盘类型(决定使用哪种键盘)
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        keyboardType="number-pad"
                    ></TextInput>
                </View>
            );
        }
    });

效果:

设置键盘类型.gif

  • multiline:如果值为真,文本输入可以输入多行,默认值为假
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        multiline={true}
                    ></TextInput>
                </View>
            );
        }
    });

效果:

多行输入.gif

  • password:如果值为真,文本输入框就成为一个密码区域,默认值为假
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        password={true}
                    ></TextInput>
                </View>
            );
        }
    });

效果:

密码模式.gif

  • placeholder:在文本输入之前提示用户文本框功能,也就是占位文字
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="请输入账号"
                    ></TextInput>
                </View>
            );
        }
    });

效果:

占位文字.gif

  • placeholderTextColor:占位字符串的文本颜色
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="请输入账号"
                        placeholderTextColor="red"
                    ></TextInput>
                </View>
            );
        }
    });

效果:

占位文字颜色.gif

  • autoCapitalize:控制TextInput是否要自动将特定字符切换为大写

    • none:不自动使用任何东西
    • sentences:每个句子的首字母(默认)
    • words:每一个单词的首字母
    • characters:所有字符
        var textInputTest = React.createClass({
            render(){
                return(
                    <View style={styles.container}>
                        {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="none"
                        autoCapitalize="none"
                    ></TextInput>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="sentences"
                        autoCapitalize="sentences"
                    ></TextInput>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="words"
                        autoCapitalize="words"
                    ></TextInput>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        placeholder="characters"
                        autoCapitalize="characters"
                    ></TextInput>
                    </View>
                );
            }
        });
    

    效果:
    autoCapitalize.gif

  • autoCorrect:如果为false,会关闭拼写自动修正。默认值是true。

    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="没有自动改正拼写"
                    autoCorrect={false}
                ></TextInput>
                {/* 文本输入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="自动改正拼写"
                    autoCorrect={true}
                ></TextInput>
                </View>
            );
        }
    });

效果:

autoCorrect.gif

  • autoFocus:如果为true,在componentDidMount后会获得焦点。默认值为false。
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        autoFocus={true}
                    ></TextInput>
                </View>
            );
        }
    });

效果:

autoFocus.gif

  • clearButtonMode:清除按钮出现的时机

    • never:不出现
    • while-editing:编辑的时候出现
    • unless-editing:没有编辑时出现
    • always:总是出现
        var textInputTest = React.createClass({
            render(){
                return(
                    <View style={styles.container}>
                        {/* 文本输入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="never"
                    clearButtonMode="never"
                ></TextInput>
                {/* 文本输入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="while-editing"
                    clearButtonMode="while-editing"
                ></TextInput>
                {/* 文本输入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="unless-editing"
                    clearButtonMode="unless-editing"
                ></TextInput>
                {/* 文本输入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    placeholder="always"
                    clearButtonMode="always"
                ></TextInput>
                    </View>
                );
            }
        });
    

    效果:

clearButtonMode.gif

  • clearTextOnFocus:如果为true,每次开始输入的时候都会清除文本框的内容

        var textInputTest = React.createClass({
            render(){
                return(
                    <View style={styles.container}>
                        {/* 文本输入框 */}
                        <TextInput
                            style={styles.textInputStyle}
                            clearTextOnFocus={true}
                        ></TextInput>
                    </View>
                );
            }
        });
    

    效果:

clearTextOnFocus.gif

  • editable:如果值为假,文本是不可编辑,默认值为真
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        editable={false}
                    ></TextInput>
                </View>
            );
        }
    });

效果:

editable.gif

  • enablesReturnKeyAutomatically:如果为true,键盘会在文本框内没有文字的时候禁用确认按钮。默认值为false。
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    enablesReturnKeyAutomatically={true}
                ></TextInput>
                {/* 文本输入框 */}
                <TextInput
                    style={styles.textInputStyle}
                    enablesReturnKeyAutomatically={false}
                ></TextInput>
                </View>
            );
        }
    });

效果:

enablesReturnKeyAutomatically.gif

  • returnKeyType:决定返回键的样式

    • default
    • go
    • google
    • join
    • next
    • route
    • search
    • send
    • yahoo
    • done
    • emergency-call


        var textInputTest = React.createClass({
            render(){
                return(
                    <View style={styles.container}>
                        {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        returnKeyType="go"
                    ></TextInput>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        returnKeyType="join"
                    ></TextInput>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        returnKeyType="done"
                    ></TextInput>
                    </View>
                );
            }
        });
    

    效果:
    returnKeyType.gif

  • secureTextEntry:如果值为真,文本输入框就会使输入的文本变模糊,以便于像密码这样敏感的文本保持安全,类似 password 属性,默认值为假

    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        keyboardType="number-pad"
                    ></TextInput>
                </View>
            );
        }
    });

效果:
secureTextEntry.gif

  • onChange:当文本框内容变化时调用此回调函数
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        onChange={() => {alert('文本框内容改变')}}
                    ></TextInput>
                </View>
            );
        }
    });

效果:
onChange.gif

  • onChangeText:当文本框内容变化时调用此回调函数。改变后的文字内容会作为参数传递
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        onChangeText={(Text) => {alert('文字改变')}}
                    ></TextInput>
                </View>
            );
        }
    });

效果:
onChangeText.gif

  • onFocus:当文本框获得焦点的时候调用此回调函数
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        onFocus={() => {alert('文本框获得焦点')}}
                    ></TextInput>
                </View>
            );
        }
    });

效果:
onFoucs.gif

  • onBlur:当文本框失去焦点的时候调用此回调函数
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        onBlur={() => {alert('失去焦点')}}
                    ></TextInput>
                </View>
            );
        }
    });

效果:
onBlur.gif

  • onEndEditing:结束编辑时,调用回调函数
    var textInputTest = React.createClass({
        render(){
            return(
                <View style={styles.container}>
                    {/* 文本输入框 */}
                    <TextInput
                        style={styles.textInputStyle}
                        onEndEditing={() => {alert('结束文本编辑')}}
                    ></TextInput>
                </View>
            );
        }
    });

效果:

onEndEditing.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值