Android监听软键盘状态

以下代码来自于一位热心的大佬之手,感谢大佬帮忙解决问题!!!

有时候会有些特殊的情况需要监听软件盘的 弹出状态! 基本上都是 在软键盘弹出的时候,根据布局高度的变化来判断 软键盘的状态!

注意:有时候为了防止软键盘将底部导航栏顶起,需要在Activity中设置一下属性

android:windowSoftInputMode="adjustPan|stateHidden" 

但是“adjustPan” 会导致方式一失效,此时方式二课完美解决。


方式一:普通情况OK

public class KeyboardChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {

private static final String TAG = "ListenerHandler";

private View mContentView;

private int mOriginHeight;

private int mPreHeight;

private KeyBoardListener mKeyBoardListen;

public interface KeyBoardListener {

/**

* call back

* @param isShow true is show else hidden

* @param keyboardHeight keyboard height

*/

void onKeyboardChange(boolean isShow, int keyboardHeight);

}

public void setKeyBoardListener(KeyBoardListener keyBoardListen) {

this.mKeyBoardListen = keyBoardListen;

}

public KeyboardChangeListener(Activity contextObj) {

if (contextObj == null) {

Log.i(TAG, "contextObj is null");

return;

}

mContentView = findContentView(contextObj);

if (mContentView != null) {

addContentTreeObserver();

}

}

private View findContentView(Activity contextObj) {

return contextObj.findViewById(android.R.id.content);

}

private void addContentTreeObserver() {

mContentView.getViewTreeObserver().addOnGlobalLayoutListener(this);

}

@Override

public void onGlobalLayout() {

int currHeight = mContentView.getHeight();

if (currHeight == 0) {

Log.i(TAG, "currHeight is 0");

return;

}

boolean hasChange = false;

if (mPreHeight == 0) {

mPreHeight = currHeight;

mOriginHeight = currHeight;

} else {

if (mPreHeight != currHeight) {

hasChange = true;

mPreHeight = currHeight;

} else {

hasChange = false;

}

}

if (hasChange) {

boolean isShow;

int keyboardHeight = 0;

if (mOriginHeight == currHeight) {

//hidden

isShow = false;

} else {

//show

keyboardHeight = mOriginHeight - currHeight;

isShow = true;

}

if (mKeyBoardListen != null) {

mKeyBoardListen.onKeyboardChange(isShow, keyboardHeight);

}

}

}

@SuppressLint("NewApi")

public void destroy() {

if (mContentView != null) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

mContentView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

}

}

}

}


使用方法:



方式二:普通情况,以及特殊情况OK

*/public class KeyboardStatusDetector {    private static final int SOFT_KEY_BOARD_MIN_HEIGHT = 100;    private KeyboardVisibilityListener mVisibilityListener;    boolean keyboardVisible = false;    public KeyboardStatusDetector registerFragment(Fragment f) {    return registerView(f.getView());    }    public KeyboardStatusDetector registerActivity(Activity a) {        return registerView(a.getWindow().getDecorView().findViewById(android.R.id.content));    }    public KeyboardStatusDetector registerView(final View v) {        v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                Rect r = new Rect();                v.getWindowVisibleDisplayFrame(r);                int heightDiff = v.getRootView().getHeight() - (r.bottom - r.top);                if (heightDiff > SOFT_KEY_BOARD_MIN_HEIGHT) { // if more than 100 pixels, its probably a keyboard...                    if (!keyboardVisible) {                        keyboardVisible = true;                        if (mVisibilityListener != null) {                            mVisibilityListener.onVisibilityChanged(true);                        }                    }                } else {                    if (keyboardVisible) {                        keyboardVisible = false;                        if (mVisibilityListener != null) {                            mVisibilityListener.onVisibilityChanged(false);                        }                    }                }            }        });        return this;    }    public KeyboardStatusDetector setmVisibilityListener(KeyboardVisibilityListener listener) {        mVisibilityListener = listener;        return this;    }    public interface KeyboardVisibilityListener {        void onVisibilityChanged(boolean keyboardVisible);    }}

使用方式:


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容