'keyboardDidHide'返回null而不是对象

时间:2019-02-18 14:10:12

标签: react-native keyboard react-native-android

我目前正在开发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

什么会导致我的问题?非常感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

这是Android中的预期行为。如果查看显示/隐藏键盘时调用的基础native代码,您会看到返回到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'将返回一个对象。