react-native - '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
。
什么可能导致我的问题?非常感谢你的帮助!!
解决方案
这是 中的预期行为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'
返回一个对象。
推荐阅读
- javascript - 使用javascript重定向时如何找到重定向页面
- c - GtkApplication 的初始化——我应该使用 GObject 的“init”还是 GtkApplication 的“startup”?
- c# - 如何根据列表中的其他属性对列表进行排序
- matlab - MATLAB Mex C API 检查 mxarray 是否为基类
- reactjs - ReactJS,componentWillReceiveProps可以获取数据,但是render不能
- laravel - carregar múltiplos modelos em um 控制器
- actions-on-google - 智能家居 - 正确的 JSON 以在设备离线时响应 EXECUTE
- concourse - 如何测试大厅自定义资源类型?
- java - 我需要一些关于带有 hashmap 的用户类型的建议
- docker - 如何使用 docker-compose 从多个私有注册表中提取?