zoukankan      html  css  js  c++  java
  • 监听虚拟键盘弹出的高度

    原理

    监听DecorView的可见高度,当虚拟键盘弹出的时候,DecorView的可见高度会变小,这时拿android.R.id.content控件的高度-可见矩形的bottom得到的就是虚拟键盘的高度,代码如下:

    public interface KeyboardListener {
        void onKeyboardChange(boolean isPopup, int keyboardHeight);import android.app.Activity;
    import android.content.Context;
    import android.graphics.Rect;
    import android.view.View;
    import android.view.ViewTreeObserver;
    import android.widget.FrameLayout;
    
    public class KeyboardUtils implements ViewTreeObserver.OnGlobalLayoutListener {
    
        private KeyboardListener listener;
        private int mTempKeyboardHeight;
        private int navigationBarHeight;
        private Activity activity;
        private View decorView;
    
        public KeyboardUtils(Activity activity,KeyboardListener listener){
            this.activity = activity;
            this.listener = listener;
            decorView = activity.getWindow().getDecorView();
            navigationBarHeight=getNavigationBarHeight(activity);
    
        }
    
        public void enable(){
            if(activity!=null) {
                activity.getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(this);
            }
        }
    
        public void disable(){
            if (activity != null) {
                activity.getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        }
    
    
        @Override
        public void onGlobalLayout() {
            if (activity != null) {
                Rect r = new Rect();
                FrameLayout contentView = decorView.findViewById(android.R.id.content);
                decorView.getWindowVisibleDisplayFrame(r);
    int screenHeight= contentView.getRootView().getHeight(); int keyboardHeight = screenHeight-r.bottom-navigationBarHeight; if (keyboardHeight != mTempKeyboardHeight) { mTempKeyboardHeight=keyboardHeight; boolean isPopup=false; if (keyboardHeight > navigationBarHeight) { isPopup = true; } if (keyboardHeight < 0) { keyboardHeight = 0; } if (listener != null) { listener.onKeyboardChange(isPopup, keyboardHeight); } } } } private int getNavigationBarHeight(Context context){ int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId>0) { return context.getResources().getDimensionPixelSize(resourceId); } else { return 0; } } }

    使用

    在Activity中添加以下代码:

      KeyboardUtils keyboardUtils=new KeyboardUtils(this, (isPopup, keyboardHeight) -> {
                Log.d("KeyboardUtils", "onKeyboardChange() called with: isPopup = [" + isPopup + "], keyboardHeight = [" + keyboardHeight + "]");
            });
            keyboardUtils.enable();
  • 相关阅读:
    sql注入漏洞详解
    HTTP1.0/1.1/2.0的区别
    http协议详解
    LRU经典算法的原理与实现
    [译转]深入理解LayoutInflater.inflate()
    Touch事件分发机制
    重要:Android绘图只Mask遮罩
    Android View学习Tips
    ViewPager学习及使用(一)
    Android 实现瀑布流的两种思路
  • 原文地址:https://www.cnblogs.com/rainboy2010/p/12215093.html
Copyright © 2011-2022 走看看