通过阅读Rn插件的源码,发现在Android11系统以上,当Activity的android:windowSoftInputMode="adjustNothing"这种模式下可以通过这种方法获取软键盘的高度
rootLayout = (ViewGroup) getWindow().getDecorView();
rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@RequiresApi(api = Build.VERSION_CODES.R)
@Override
public void onGlobalLayout() {
rootLayout.getWindowVisibleDisplayFrame(VoiceInputActivity.this.mVisibleViewArea);
WindowInsets rootInsets =rootLayout.getRootWindowInsets();
if (rootInsets != null) {
boolean keyboardIsVisible = rootInsets.isVisible(WindowInsets.Type.ime());
if (keyboardIsVisible != VoiceInputActivity.this.mKeyboardIsVisible) {
VoiceInputActivity.this.mKeyboardIsVisible = keyboardIsVisible;
if (keyboardIsVisible) {
Insets imeInsets = rootInsets.getInsets(WindowInsets.Type.ime());
Insets barInsets = rootInsets.getInsets(WindowInsets.Type.systemBars());
int height = imeInsets.bottom - barInsets.bottom;
int softInputMode = VoiceInputActivity.this.getWindow().getAttributes().softInputMode;
int screenY = softInputMode == 48 ? VoiceInputActivity.this.mVisibleViewArea.bottom - height : VoiceInputActivity.this.mVisibleViewArea.bottom;
Log.d(TAG, "软键盘弹起 onGlobalLayout: " + height + " , screenY: " + screenY);
// ReactRootView.this.sendEvent("keyboardDidShow", this.createKeyboardEventPayload((double)PixelUtil.toDIPFromPixel((float)screenY), (double)PixelUtil.toDIPFromPixel((float)this.mVisibleViewArea.left), (double)PixelUtil.toDIPFromPixel((float)this.mVisibleViewArea.width()), (double)PixelUtil.toDIPFromPixel((float)height)));
} else {
// ReactRootView.this.sendEvent("keyboardDidHide", this.createKeyboardEventPayload((double)PixelUtil.toDIPFromPixel((float)ReactRootView.this.mLastHeight), 0.0, (double)PixelUtil.toDIPFromPixel((float)this.mVisibleViewArea.width()), 0.0));
Log.d(TAG, "软键盘收起");
}
}
}
}
});