ReactNative键盘无法收起的问题

问题描述:

            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];

    });

}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值