问题描述:
ReactNative版本0.52.0,当页面有连续多个TextInput时,切换焦点,键盘无法收起。
在react-native中有TextInputState这个类,记录了当前获取焦点的TextInput的textFieldID,用来和原生进行交互。经过试验发现,多个TextInput切换焦点后,调用关闭键盘的方法,textFieldID为null,个人认为是切换焦点时所记录的状态没有修改所导致的。具体为什么没有修改,技术水评限制没有找到原因。
blurTextInput: function(textFieldID: ?number)在这个方法中可以看,所传入的textFieldID为null,所以才无法收起键盘。
/** * @param {number} TextInputID id of the text field to focus * Focuses the specified text field * noop if the text field was already focused */ focusTextInput: function(textFieldID: ?number) { if (this._currentlyFocusedID !== textFieldID && textFieldID !== null) { this._currentlyFocusedID = textFieldID; if (Platform.OS === 'ios') { UIManager.focus(textFieldID); } else if (Platform.OS === 'android') { UIManager.dispatchViewManagerCommand( textFieldID, UIManager.AndroidTextInput.Commands.focusTextInput, null ); } } }, /** * @param {number} textFieldID id of the text field to unfocus * Unfocuses the specified text field * noop if it wasn't focused */ blurTextInput: function(textFieldID: ?number) { if (this._currentlyFocusedID === textFieldID && textFieldID !== null) { this._currentlyFocusedID = null; if (Platform.OS === 'ios') { UIManager.blur(textFieldID); } else if (Platform.OS === 'android') { UIManager.dispatchViewManagerCommand( textFieldID, UIManager.AndroidTextInput.Commands.blurTextInput, null ); } } } };
本人的解决方法是,导出一个原生方法,强制收起键盘,无法收起键盘时调用此方法强制收起。
RCT_EXPORT_METHOD(endEditing) {
dispatch_async(dispatch_get_main_queue(), ^{
[[[[[UIApplication sharedApplication] delegate] window] rootViewController].view endEditing:YES];
});
}