Android監(jiān)聽軟鍵盤狀態(tài)

以下代碼來自于一位熱心的大佬之手,感謝大佬幫忙解決問題!!!

有時候會有些特殊的情況需要監(jiān)聽軟件盤的 彈出狀態(tài)! 基本上都是 在軟鍵盤彈出的時候,根據(jù)布局高度的變化來判斷 軟鍵盤的狀態(tài)!

注意:有時候為了防止軟鍵盤將底部導航欄頂起,需要在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);? ? }}

使用方式:


最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內(nèi)部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,537評論 0 17
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,973評論 19 139
  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,775評論 0 33
  • Android 中自定義軟鍵盤 \ \ \ 圖一為搜狗輸入法、圖二為自定義密碼鍵盤、圖三為自定義密碼鍵盤 java...
    來來來來看天上閱讀 912評論 0 0