TextInput输入框 右侧添加按钮 如:删除

TextInput文本框右侧显示“清除”按钮 。

这里写图片描述
在RN api中有介绍

clearButtonMode enum('never', 'while-editing', 'unless-editing', 'always')  
显示“清除”按钮

但是只是适用于ios系统
为了在Android系统正常适用,写组件来适配。

思路:
需要按钮显示和隐藏卡,就需要监听TextInput 输入的内容,如果内容为空不显示按钮,如果内容不为空则显示按钮。
实现:

// 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            inputValue: "",
        };
        this.onChange = this._onChange.bind(this);
    }

     _onChange(changeText) {
        this.setState({
            inputValue: changeText,
        });
    }

    render() {
        let {inputValue} = this.state;
        return (
            <View style={styles.container}>
                <TextInput
                    clearButtonMode={'never'}
                    value={inputValue}
                    onChangeText={this.onChange}/>
                {this._isNull(inputValue) ? null : this._getRightButtonView()}
            </View>
        );
    }

    _isNull(str) {
         let result = true;
        if (str === "" || str === undefined) {
            result = true;
        }

        if (str.length > 0) {
            result = false;
        }
        return result;
    }

    _getRightButtonView() {
        //右侧按钮图
        let source = this.props.source ? this.props.source : require('../img/input_close.png');
        return (
            <TouchableOpacity activeOpacity={0.5}
                              style={styles.closeOpacityStyle}
                              onPress={() => {
                                  this.setState({inputValue: ""})
                              }}>
                <Image style={styles.closeStyle}
                       source={source}/>
            </TouchableOpacity>)
    }

方法实现,但是不好复用,单独写成一个组件会好些。
问题来了 :如右侧不是清除,有可能是问号“?”,这些问题我们是否需要考虑

贴代码

'use strict';
import React, {Component, PropTypes} from "react";

import {
    StyleSheet,//样式
    View,//视图组件;
    Image,//图片
    TextInput,//输入框
    TouchableOpacity,//一个类似button的组件
} from "react-native";

export default class TextInputRigthButton extends Component {

// 传递参数属性定义
    static PropTypes = {
        onButtonClick: PropTypes.func.isRequired
    }

    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            inputValue: "",
        };
        this.onChange = this._onChange.bind(this);
    }

    _isNull(str) {
         let result = true;
        if (str === "" || str === undefined) {
            result = true;
        }

        if (str.length > 0) {
            result = false;
        }
        return result;
    }


    render() {
        let {inputValue} = this.state;
        return (
            <View style={styles.container}>
                <TextInput
                    underlineColorAndroid="transparent"
                    numberOfLines={1}
                    clearButtonMode={'never'}
                    maxLength={50}
                    value={inputValue}
                    onChangeText={this.onChange}
                    {...this.props}/>
                {this._isNull(inputValue) ? null : this._getRightButtonView()}
            </View>
        );
    }

    _getRightButtonView() {
        //右侧按钮图 
        //自定义 按钮图
        let source = this.props.source ? this.props.source : require('../../img/input_close.png');
        return (
            <TouchableOpacity activeOpacity={0.5}
                              style={styles.closeOpacityStyle}
                              onPress={() => {
                                  this.props.onButtonClick();
                              }}>
                <Image style={styles.closeStyle}
                       source={source}/>
            </TouchableOpacity>)
    }

    //清除输入款中的值
    clearInputValue() {
        this.setState({inputValue: ""})
    }

    //获取输入款中的值
    getInputValue() {
        return this.state.inputValue;
    }

    _onChange(changeText) {
        this.setState({
            inputValue: changeText,
        });
    }

}

const styles = StyleSheet.create(
    {
        container: {
            flex: 1,
            flexDirection: 'row',
            alignItems: 'center'
        },
        closeStyle: {
            height: 18,
            width: 18,
        },
        closeOpacityStyle: {
            height: 54,
            width: 54,
            justifyContent: 'center',
        },
    }
)

使用

 <TextInputRigthButton
        numberOfLines={1}
        underlineColorAndroid="transparent"
        maxLength={50}
        style={{
            fontSize: 16,
            color: "#333333",
            width: width - DP.dp11 * 3 - DP.dp40,//给TextInput设置宽度,根据需要来
        }}
        ref={(c) => this.email = c}
        onButtonClick={()=>{
            //根据需要自己控制
            this.email.clearInputValue();
        }}
        placeholder={"请输入您的邮箱"}
        placeholderTextColor={"#999999"}/>

希望对您有帮助。。。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值