react-native-navigation的迁移库

KeyboardVisibilityDetector.java 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.reactnativenavigation.utils;
  2. import android.content.Context;
  3. import android.graphics.Rect;
  4. import android.view.View;
  5. import android.view.ViewTreeObserver;
  6. import android.view.inputmethod.InputMethodManager;
  7. import com.reactnativenavigation.NavigationApplication;
  8. public class KeyboardVisibilityDetector {
  9. // 0.15 ratio is perhaps enough to determine keypad height.
  10. public static final double KEYBOARD_VISIBLE_RATIO = 0.15;
  11. private final KeyboardVisibilityLayoutListener keyboardVisibilityListener;
  12. private final View screen;
  13. private Runnable keyboardCloseListener;
  14. public KeyboardVisibilityDetector(final View screen) {
  15. this.screen = screen;
  16. keyboardVisibilityListener = new KeyboardVisibilityLayoutListener(this);
  17. screen.getViewTreeObserver().addOnGlobalLayoutListener(keyboardVisibilityListener);
  18. }
  19. public boolean isKeyboardVisible() {
  20. return keyboardVisibilityListener.isKeyboardVisible();
  21. }
  22. public void setKeyboardCloseListener(Runnable keyboardCloseListener) {
  23. this.keyboardCloseListener = keyboardCloseListener;
  24. }
  25. public void closeKeyboard() {
  26. InputMethodManager imm = (InputMethodManager) screen.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  27. imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
  28. }
  29. private static class KeyboardVisibilityLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
  30. public static final int KEYBOARD_CLOSE_DURATION = 100;
  31. private View screen;
  32. private boolean isVisible = false;
  33. private KeyboardVisibilityDetector detector;
  34. public KeyboardVisibilityLayoutListener(KeyboardVisibilityDetector detector) {
  35. this.detector = detector;
  36. this.screen = detector.screen;
  37. }
  38. public boolean isKeyboardVisible() {
  39. return isVisible;
  40. }
  41. @Override
  42. public void onGlobalLayout() {
  43. int screenHeight = screen.getRootView().getHeight();
  44. int screenBottomY = getScreenBottomY(screen);
  45. int keyboardHeight = screenHeight - screenBottomY;
  46. if (isKeyboardVisible(screenHeight, keyboardHeight)) {
  47. isVisible = true;
  48. } else {
  49. if (isVisible && detector.keyboardCloseListener != null) {
  50. NavigationApplication.instance.runOnMainThread(detector.keyboardCloseListener, KEYBOARD_CLOSE_DURATION);
  51. }
  52. isVisible = false;
  53. }
  54. }
  55. private boolean isKeyboardVisible(int screenHeight, int keypadHeight) {
  56. return keypadHeight > screenHeight * KEYBOARD_VISIBLE_RATIO;
  57. }
  58. private int getScreenBottomY(View screen) {
  59. Rect r = new Rect();
  60. screen.getWindowVisibleDisplayFrame(r);
  61. return r.bottom;
  62. }
  63. }
  64. }