wjc
2025-03-04 e5ca9f9fb28d65a71dc14c19e87098c62141419e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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);
 
    }
 
}