如何检测键盘何时在无调整模式下启动?



我想在android:windowSoftInputMode="adjustNothing"时检查键盘何时启动。 在此状态下,此处的链接对我不起作用。

如果将其设置为 adjustNothing,则仍然无法使用他的代码片段检测键盘是否可见。要解决此问题,请检查下面的代码。

View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
... do something here
}
}
}); 

最新更新