android软键盘弹出隐藏的监听

时间:2023-03-08 20:48:12
android软键盘弹出隐藏的监听

通过网上搜索关于软键盘的隐藏弹出的监听,有几种方式,其中最有效的方式是在View的Onlayout()里面做文章

具体代码:

将布局视图自定义,重写onlayout()方法,然后在主Activity里面调用接口就可以了

/**
* 为了监听软键盘的弹出隐藏
*
*/
public class ResizeLayout extends LinearLayout {
private InputListener mListener; public interface InputListener {
void OnInputListener(boolean isHideInput);
} public void setOnResizeListener(InputListener l) {
mListener = l;
} public ResizeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
} private boolean mHasInit = false;
private boolean mHasKeyboard = false;
private int mHeight; @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.onLayout(changed, l, t, r, b);
if (!mHasInit) {
mHasInit = true;
mHeight = b;
System.out.println("mHeight= " + b);
}
else {
mHeight = mHeight < b ? b : mHeight;
} if (mHasInit && mHeight > b) { // mHeight代表键盘的真实高度 ,b代表在窗口中的高度 mHeight>b
mHasKeyboard = true;
mListener.OnInputListener(false);
}
if (mHasInit && mHasKeyboard && mHeight == b) { // mHeight = b
mHasKeyboard = false;
mListener.OnInputListener(true);
}
}