New file |
| | |
| | | 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); |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | |