From a1ac20d9cbe90b566bffe3ed39a6e07700c3248f Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期一, 19 六月 2023 10:30:06 +0800
Subject: [PATCH] Merge branch 'wjc'

---
 app/src/main/java/com/hdl/photovoltaic/utils/KeyboardStateObserverUtils.java |  108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 108 insertions(+), 0 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/utils/KeyboardStateObserverUtils.java b/app/src/main/java/com/hdl/photovoltaic/utils/KeyboardStateObserverUtils.java
new file mode 100644
index 0000000..408e86e
--- /dev/null
+++ b/app/src/main/java/com/hdl/photovoltaic/utils/KeyboardStateObserverUtils.java
@@ -0,0 +1,108 @@
+package com.hdl.photovoltaic.utils;
+
+import android.app.Activity;
+import android.graphics.Rect;
+import android.view.View;
+import android.view.ViewTreeObserver;
+import android.widget.FrameLayout;
+
+/**
+ * 閿洏鐘舵�佽瀵熷憳
+ */
+public class KeyboardStateObserverUtils {
+    private static final String TAG = KeyboardStateObserverUtils.class.getSimpleName();
+
+    public static KeyboardStateObserverUtils getKeyboardStateObserver(Activity activity) {
+        return new KeyboardStateObserverUtils(activity);
+
+    }
+
+    private final View mChildOfContent;
+
+    private int usableHeightPrevious;//浣跨敤楂樺害
+
+    private OnKeyboardVisibilityListener listener;
+
+    public void setKeyboardVisibilityListener(OnKeyboardVisibilityListener listener) {
+        this.listener = listener;
+
+    }
+
+    private KeyboardStateObserverUtils(Activity activity) {
+//        activity.getWindow().findViewById(window);
+        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
+
+        mChildOfContent = content.getChildAt(0);
+
+        //鑾峰彇瑙嗗浘鏍戣瀵熻��,娣诲姞鍏ㄥ眬甯冨眬鐩戝惉鍣�
+        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
+            public void onGlobalLayout() {
+                possiblyResizeChildOfContent();
+
+            }
+
+        });
+
+    }
+
+    /**
+     * 閲嶆柊璁剧疆ChildOfContent鐨勫ぇ灏�
+     */
+    private void possiblyResizeChildOfContent() {
+        int usableHeightNow = computeUsableHeight();
+
+        if (usableHeightNow != usableHeightPrevious) {
+            //鑾峰彇鏁翠釜灞忓箷鐨勯珮搴�
+            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
+
+            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
+
+            if (heightDifference > (usableHeightSansKeyboard / 4)) {
+                if (listener != null) {
+                    listener.onKeyboardShow(heightDifference);
+
+                }
+
+            } else {
+                if (listener != null) {
+                    listener.onKeyboardHide(heightDifference);
+
+                }
+
+            }
+
+            usableHeightPrevious = usableHeightNow;
+
+            //Log.d(TAG,"usableHeightNow: " + usableHeightNow + " | usableHeightSansKeyboard:" + usableHeightSansKeyboard + " | heightDifference:" + heightDifference);
+
+        }
+
+    }
+
+    /**
+     * 璁$畻鍙敤楂樺害
+     *
+     * @return 楂樺害
+     */
+    private int computeUsableHeight() {
+        Rect r = new Rect();
+
+        //鑾峰彇鍙互鏄剧ず鐨勫尯鍩�
+        mChildOfContent.getWindowVisibleDisplayFrame(r);
+
+        //Log.d(TAG,"rec bottom>" + r.bottom + " | rec top>" + r.top);
+        return (r.bottom - r.top);// 鍏ㄥ睆妯″紡涓嬶細 return r.bottom
+
+    }
+
+    public interface OnKeyboardVisibilityListener {
+        void onKeyboardShow(int h);
+
+        void onKeyboardHide(int h);
+
+    }
+
+}
+
+
+

--
Gitblit v1.8.0