private void listenKeyboardVisible() {
finl View contentView = findViewById(android.R.id.content);
final View activityRoot = getWindow().getDecorView();
if (activityRoot ==null) {
return;
}
contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
private final Rectr =new Rect();
private final int visibleThreshold = Math.round(ConvertUtils.dp2px(100));
@Override
public void onGlobalLayout() {
activityRoot.getWindowVisibleDisplayFrame(r);
int heightDiff =activityRoot.getRootView().getHeight() -r.height();
//键盘是否弹出
boolean isOpen = heightDiff >visibleThreshold;
}
});
}
网友评论