我正在使用自定义键盘。我使用 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()
它对我有用。您可以为 Textinput
的 onFocus
编写一个函数,并在该函数中关闭默认键盘,使用自定义键盘并再次将焦点放在 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/