以下代碼來自于一位熱心的大佬之手,感謝大佬幫忙解決問題!!!
有時候會有些特殊的情況需要監聽軟件盤的 彈出狀態! 基本上都是 在軟鍵盤彈出的時候,根據布局高度的變化來判斷 軟鍵盤的狀態!
注意:有時候為了防止軟鍵盤將底部導航欄頂起,需要在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);? ? }}
使用方式: