Skip to content

Instantly share code, notes, and snippets.

@tuoxie007
Created April 11, 2014 13:18
Show Gist options
  • Save tuoxie007/10468136 to your computer and use it in GitHub Desktop.
Save tuoxie007/10468136 to your computer and use it in GitHub Desktop.
监听键盘显示和隐藏事件
final View activityRootView = getWindow().getDecorView().findViewById(R.id.post_editor_dialog);
if (activityRootView != null) {
ViewTreeObserver observer = activityRootView.getViewTreeObserver();
if (observer != null) {
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
int lastHeightDiff;
@Override
public void onGlobalLayout() {
View rootView = activityRootView.getRootView();
if (rootView != null) {
int heightDiff = rootView.getHeight() - activityRootView.getHeight();
if (heightDiff != lastHeightDiff) {
if (heightDiff > 200) {
// onKeyboardShow();
} else {
// onKeyboardHide();
}
lastHeightDiff = heightDiff;
}
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment