解决React Native中Textinput无法输入、失去焦点、placeholder默认占位符不显示问题

解决React Native中Textinput无法输入、失去焦点、placeholder切换占位符不显示问题

最近写代码的时候遇到了一个很奇怪的问题,点击输入框无法输入文字,光标也不显示,但是点击后弹出的软键盘还在。

用到的组件是react-native中的Textinput来获取用户输入的文本信息。

问题排查

代码片段

import React, { useState } from 'react';
import { View, Text, TextInput, StyleSheet } from 'react-native';

const YourComponent = () => {
    const [isNotShowPlaceholder, setIsNotShowPlaceholder] = useState(false);

    const onClickCallback = (eventType, text = null) => {
        if (eventType === 'onFocus') {
            onFoucsn();
        } else if (eventType === 'onBlur') {
            onBlur();
        } else if (eventType === 'onChangeText') {
            onChangeText(text);
        }
    };

	//获取焦点时执行
    const onFoucsn = () => {
        setIsNotShowPlaceholder(true);
        console.log("获取到了焦点!");
    };

	//失去焦点时执行
    const onBlur = () => {
        setIsNotShowPlaceholder(false); 
        console.log("失去了焦点!");
    };

	//处理用户输入的内容
    const onChangeText = (text) => {
        console.log(text);
    };

    return (
        <View style={styles.container}>
            <Text style={styles.leftText}>用户名</Text>
            <TextInput
                style={styles.rightInput}
                placeholder={isNotShowPlaceholder ? '' : '请输入用户名称'}
                keyboardType='default'
                onFocus={() => onClickCallback('onFocus')}
                onBlur={() => onClickCallback('onBlur')}
                onChangeText={(text) => onClickCallback('onChangeText', text)}
            />
        </View>
    );
};

const styles = StyleSheet.create({
    container: {
        flexDirection: 'row',
        justifyContent: 'space-between',
        width: 600,
    },
    leftText: {
        fontSize: 40,
    },
    rightInput: {
        textAlign: 'right',
        fontSize: 40,
    },
});

export default YourComponent;

预期效果是当用户点击输入框,获取到焦点,用户输入信息,进行信息处理,用户点击输入框之外的内容则失去焦点。

但是实际上的效果却是:用户点击输入框后,日志提示“获取到了焦点!”并弹出了软键盘,但是立刻又提示“失去了焦点!”而且无法输入信息。

最后排查发现是外层容器View的样式flexDirection: 'row'导致的。

因为在点击输入框时会进行占位符的内容切换placeholder={isNotShowPlaceholder ? '' : '请输入用户名称'}可能是由于不同安卓系统对于空字符串的处理方式不同,导致输入框失焦。在安卓8的系统上可以正常输入,但是安卓14上就会出现问题。

解决方法

方法一:给Textinput设置一个固定的宽度,这样就不会由于占位符改变成空字符串而失焦

rightInput: {
		width:500
        textAlign: 'right',
        fontSize: 40,
    },

方法二:将空字符串替换成空格字符' ',在视觉上可以实现类似空字符串的效果

placeholder={isNotShowPlaceholder ? ' ' : '请输入用户名称'}
  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过在组件外部包裹一个TouchableWithoutFeedback组件来实现。具体做法如下: 1. 在组件的render方法,将所有需要关闭键盘的TextInput组件的ref存储到一个数组。 2. 在组件外部包裹一个TouchableWithoutFeedback组件,并给它设置onPress事件处理函数。 3. 在onPress事件处理函数,遍历存储了TextInput组件ref的数组,并分别调用其blur方法来使其失去焦点。 代码示例: ``` import React, { Component } from 'react'; import { TouchableWithoutFeedback, Keyboard } from 'react-native'; class MyComponent extends Component { constructor(props) { super(props); this.textInputRefs = []; } render() { return ( <TouchableWithoutFeedback onPress={Keyboard.dismiss}> {/* 这里是你的组件内容 */} </TouchableWithoutFeedback> ); } componentDidMount() { this.textInputRefs.forEach(ref => { if (ref && ref.current) { ref.current.blur(); } }); } // 将TextInput的ref存储到数组 addTextInputRef = ref => { if (ref && ref.props && ref.props.focusable) { this.textInputRefs.push(ref); } }; } ``` 在上面的代码,我们在组件的render方法使用了TouchableWithoutFeedback组件来包裹组件的内容,并设置了onPress事件处理函数为Keyboard.dismiss。这个函数会关闭输入键盘并使TextInput失去焦点。 在组件的componentDidMount生命周期方法,我们遍历了存储了TextInput组件ref的数组,并调用了它们的blur方法来使其失去焦点。这样,当用户点击组件以外的任何地方时,键盘就会关闭并且TextInput失去焦点。 最后,我们还定义了一个addTextInputRef方法,用于将TextInput组件的ref存储到数组。这个方法会在每个TextInput组件的ref属性被调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值