美文网首页技术池
Android监听软键盘状态

Android监听软键盘状态

作者: 大刀王五 | 来源:发表于2017-09-13 18:17 被阅读0次

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

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

注意:有时候为了防止软键盘将底部导航栏顶起,需要在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);    }}

使用方式:

相关文章

网友评论

    本文标题:Android监听软键盘状态

    本文链接:https://www.haomeiwen.com/subject/witlsxtx.html