react-native - this.refs.FieldName.focus() 不是 React Native 中的函数

标签 react-native custom-keyboard onfocus

我正在使用自定义键盘。我使用 CustomTextInput 代替 TextInput。一切正常,但我想将输入字段集中在函数中。

            <CustomTextInput customKeyboardType="hello" 
                    onFocus={() => this.onFocus('fieldname')} 
                    selectTextOnFocus={ true } 
                    ref="TextInput" 
                    underlineColorAndroid = 'transparent' 
                  onChangeText={(value) => this.setState({fieldname:this.onChange(value)})}
            />

在函数中,如下所示:-

   function () {
     Keyboard.dismiss();  
     this.refs.TextInput.focus();
   }

但我收到错误:- this.refs.TextInput.focus 不是函数。 基本上我的 focus() 函数不起作用。

请帮忙!

最佳答案

我尝试了 this.refs['TextInput'].focus() 它对我有用。您可以为 TextinputonFocus 编写一个函数,并在该函数中关闭默认键盘,使用自定义键盘并再次将焦点放在 TextInput 上。我希望它会有所帮助。

这是我的代码:

<TextInput
    style={{ height: 40, width: 200 }}
    ref="TextInput"
/>

<TouchableOpacity
    onPress={() => { (this.refs['TextInput'] as any).focus() }}>
        <Text>Press to focus</Text>
</TouchableOpacity>

关于react-native - this.refs.FieldName.focus() 不是 React Native 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50581072/

相关文章:

React-native-android - 如何将图像保存到 Android 文件系统并在手机的 'Gallery' 中查看

navigation - 文档可见性变化与窗口模糊/焦点,有什么区别,何时使用哪个?

javascript - 如何使用 jQuery 在按钮按下时在文本区域中向上、向下、向左、向右移动光标?

android - 为 android 虚拟键盘设置 keyPreviewLayout 导致崩溃

iOS/Swift 从右侧而不是底部打开键盘

javascript - 焦点和模糊事件在错误的元素上触发

Javascript 调用在运行时不显示 (asp.net)

react-native - Redux Saga yield call(someAPI) 不等待 API 调用完成

javascript - 我们如何在不使用 Redux 的情况下在 react-native 中实现撤消/重做、图像旋转和放大和缩小方法

javascript - 在 react-native 中使用两个构造函数