分享 监听软键盘 高度和关闭状态
这是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);
}
}