React Native 小记 - LessBorderTextInput 无边框的 TextInput

由于 TextInput 在 Android 和 iOS 平台默认表现不一致,为了统一样式,这里参照官方文档( 英文文档 | 中文文档 )进行了封装,并添加了对 ref 的支持。ref 用于获取组件,实现自动切换输入框的焦点等场景。

如果移动端访问效果不佳,请使用 ==> Github Pages 版

代码展示

import React from 'react';
import {Platform, TextInput,} from 'react-native';

//没有底部下划线的输入框
export default class LessBorderTextInput extends React.Component {

    componentDidMount() {
        if (this.props.onRef != null) {
            this.props.onRef(this)
        }
    }

    focus() {
        this.textInput.focus()
    }

    render() {
        let mView;
        if (Platform.OS === 'android') {
            mView =
                <TextInput
                    {...this.props}
                    ref={input => this.textInput = input}
                    underlineColorAndroid={"transparent"}
                />;
        } else {
            mView =
                <TextInput
                    {...this.props}
                    ref={input => this.textInput = input}
                />;
        }
        return mView;
    }
}

总结

基本实现思路是根据平台的不同,调用平台特有的属性来统一显示效果,再在使用的时候,外层嵌套 View 来实现统一样式的底部边框,还能添加类似密码点击可见等效果。

今天10.24 程序员节了,祝各位节日快乐。

如果有什么建议或者问题可以随时联系我,共同探讨学习:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值