Skip to content

Instantly share code, notes, and snippets.

@Runly
Last active March 22, 2017 07:27
Show Gist options
  • Save Runly/5b78c6276f86fadfcfcc2504278c45c3 to your computer and use it in GitHub Desktop.
Save Runly/5b78c6276f86fadfcfcc2504278c45c3 to your computer and use it in GitHub Desktop.
判断软键盘是否弹出
public interface OnSoftKeyBoardShowListener {
void hasShow(boolean isShow);
}
/**
* 判断软键盘是否弹出
*/
private class MyOnGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
private OnSoftKeyBoardShowListener listener;
private View rootView;
MyOnGlobalLayoutListener(View rootView, OnSoftKeyBoardShowListener listener) {
this.listener = listener;
this.rootView = rootView;
}
@Override
public void onGlobalLayout() {
final Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
final int screenHeight = rootView.getRootView().getHeight();
final int heightDifference = screenHeight - rect.bottom;
boolean visible = heightDifference > screenHeight / 3;
if (listener != null)
listener.hasShow(visible);
}
}
//////////////////////////////////////
final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
MyOnGlobalLayoutListener listener = new MyOnGlobalLayoutListener(rootView,
isShow -> {
if (isShow) {
//////
} else {
/////
}
});
rootView.getViewTreeObserver().addOnGlobalLayoutListener(listener);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment