Last active
March 22, 2017 07:27
-
-
Save Runly/5b78c6276f86fadfcfcc2504278c45c3 to your computer and use it in GitHub Desktop.
判断软键盘是否弹出
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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