监听软键盘 高度和关闭状态

分享 监听软键盘 高度和关闭状态

这是github 上面上的一个开源类 可以参考https://github.com/w446108264/XhsEmoticonsKeyboard




import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

public class SoftKeyboardSizeWatchLayout extends RelativeLayout {

    private Context mContext;
    private boolean mIsSoftKeyboardPop = false;
    private int mScreenHeight = 0;
    private int mOldh = -1;
    private int mNowh = -1;

    public SoftKeyboardSizeWatchLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                ((Activity) mContext).getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                if (mScreenHeight == 0) {
                    mScreenHeight = r.bottom;
                }
                mNowh = mScreenHeight - r.bottom;
                if (mOldh != -1 && mNowh != mOldh) {
                    if (mNowh > 0) {
                        mIsSoftKeyboardPop = true;
                        if (mListenerList != null) {
                            for (OnResizeListener l : mListenerList) {
                                l.OnSoftPop(mNowh);
                            }
                        }
                    } else {
                        mIsSoftKeyboardPop = false;
                        if (mListenerList != null) {
                            for (OnResizeListener l : mListenerList) {
                                l.OnSoftClose();
                            }
                        }
                    }
                }
                mOldh = mNowh;
            }
        });
    }

    public boolean isSoftKeyboardPop() {
        return mIsSoftKeyboardPop;
    }

    private List<OnResizeListener> mListenerList;

    public void addOnResizeListener(OnResizeListener l) {
        if (mListenerList == null) {
            mListenerList = new ArrayList<>();
        }
        mListenerList.add(l);
    }

    public interface OnResizeListener {
        /**
         * 软键盘弹起
         */
        void OnSoftPop(int height);

        /**
         * 软键盘关闭
         */
        void OnSoftClose();
    }
}


umeng sdk 也提供一个监听键盘弹起关闭状态的view

public class KeyboardListenRelativeLayout extends RelativeLayout {
	
	private static final String TAG = KeyboardListenRelativeLayout.class.getSimpleName();
	
	public static final byte KEYBOARD_STATE_SHOW = -3;
	public static final byte KEYBOARD_STATE_HIDE = -2;
	public static final byte KEYBOARD_STATE_INIT = -1;
	
	//true: show;  false:hide
	private boolean lastStatus = false;
	
	private IOnKeyboardStateChangedListener onKeyboardStateChangedListener;
	
	public KeyboardListenRelativeLayout(Context context) {
		super(context);
		init();
	}
	public KeyboardListenRelativeLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public KeyboardListenRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	public void setOnKeyboardStateChangedListener(IOnKeyboardStateChangedListener onKeyboardStateChangedListener) {
		this.onKeyboardStateChangedListener = onKeyboardStateChangedListener;
	}
	
	public void init()
	{
		getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

			private int screenheight = 0;
			public int getHeight()
			{
				if(screenheight>0) return screenheight;
				WindowManager wm = (WindowManager) getContext()
	                    .getSystemService(Context.WINDOW_SERVICE);
	 
			     //int width = wm.getDefaultDisplay().getWidth();
			     int height = wm.getDefaultDisplay().getHeight();
			     screenheight = height;
			     return screenheight;
			 
			}
            public void onGlobalLayout() {
                // TODO Auto-generated method stub
                Rect r = new Rect();
                ((Activity)getContext()).getWindow().getDecorView().getWindowVisibleDisplayFrame(r); ;

                int screenHeight = getHeight();
                int heightDiff = screenHeight - (r.bottom - r.top);
                Log.d("Keyboard Size", "Size: " + heightDiff);
                boolean visible = Math.abs(heightDiff) > screenHeight / 3;
                
                if(lastStatus!=visible)
                {
                	lastStatus = visible;
                	Log.d("Keyboard", "Keyboard " + (visible?"opened":"closed"));
	                if(!visible&&onKeyboardStateChangedListener != null) {
	    				onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_HIDE);
	    			}
	                if(visible&&onKeyboardStateChangedListener != null) {
	    				onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_SHOW);
	    			}
                }
            }
        });
	}
	
	public interface IOnKeyboardStateChangedListener {
		public void onKeyboardStateChanged(int state);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亚洲小炫风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值