首页 > 解决方案 > 'keyboardDidHide' 返回 null 而不是对象

问题描述

我目前正在开发一个react-native应用程序,并且正在尝试在显示键盘时为登录屏幕的布局设置动画。

要跟踪键盘的状态,我正在使用以下代码:

componentDidMount() {
    this.keyboardDidShowSub = Keyboard.addListener('keyboardDidShow', (event) => console.log(event));
    this.keyboardDidHideSub = Keyboard.addListener('keyboardDidHide', (event) => console.log(event));
}

keyboardDidShow正在工作并返回:

Object {
   "endCoordinates": Object {
     "height": 286,
     "screenX": 0,
     "screenY": 354,
     "width": 360,
   },
}

但是,keyboardDidHide不工作和返回null

什么可能导致我的问题?非常感谢你的帮助!!

标签: react-nativekeyboardreact-native-android

解决方案


这是 中的预期行为Android。如果您查看显示/隐藏键盘时调用的底层本机代码,您可以看到发送回 javascript 端的内容。

private void checkForKeyboardEvents() {
  getRootView().getWindowVisibleDisplayFrame(mVisibleViewArea);
  final int heightDiff =
    DisplayMetricsHolder.getWindowDisplayMetrics().heightPixels - mVisibleViewArea.bottom;
  if (mKeyboardHeight != heightDiff && heightDiff > mMinKeyboardHeightDetected) {
    // keyboard is now showing, or the keyboard height has changed
    mKeyboardHeight = heightDiff;
    WritableMap params = Arguments.createMap();
    WritableMap coordinates = Arguments.createMap();
    coordinates.putDouble("screenY", PixelUtil.toDIPFromPixel(mVisibleViewArea.bottom));
    coordinates.putDouble("screenX", PixelUtil.toDIPFromPixel(mVisibleViewArea.left));
    coordinates.putDouble("width", PixelUtil.toDIPFromPixel(mVisibleViewArea.width()));
    coordinates.putDouble("height", PixelUtil.toDIPFromPixel(mKeyboardHeight));
    params.putMap("endCoordinates", coordinates);
    sendEvent("keyboardDidShow", params);
  } else if (mKeyboardHeight != 0 && heightDiff <= mMinKeyboardHeightDetected) {
    // keyboard is now hidden
    mKeyboardHeight = 0;
    sendEvent("keyboardDidHide", null); // <- you can see here that when the keyboard is hidden it sends back null
  }
}

值得注意的是iOS'keyboardWillShow','keyboardDidShow''keyboardWillHide''keyboardDidHide'返回一个对象。


推荐阅读