第一步纠俭,先上工具類事富,通過監(jiān)聽根視圖的高度變化來判斷是否顯示了軟鍵盤
public class SoftKeyBoardListener {
@SuppressLint("StaticFieldLeak")
private Activity mActivity;
private int rootViewVisibleHeight; //紀(jì)錄根視圖的顯示高度
private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
private final View rootView; //activity的根視圖
private final ViewTreeObserver viewTreeObserver;
private final ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener;
@SuppressLint("StaticFieldLeak")
private volatile static SoftKeyBoardListener instance;
public static SoftKeyBoardListener newInstance(Activity activity) {
if (instance == null) {
synchronized (SoftKeyBoardListener.class) {
if (instance == null) {
instance = new SoftKeyBoardListener(activity);
}
}
}
return instance;
}
public void clearInstance(){
mActivity = null;
if(viewTreeObserver.isAlive()){
viewTreeObserver.removeOnGlobalLayoutListener(onGlobalLayoutListener);
}
onSoftKeyBoardChangeListener = null;
instance = null;
}
private SoftKeyBoardListener(Activity activity) {
mActivity = activity;
//獲取activity的根視圖
rootView = mActivity.getWindow().getDecorView();
viewTreeObserver = rootView.getViewTreeObserver();
onGlobalLayoutListener = () -> {
//獲取當(dāng)前根視圖在屏幕上顯示的大小
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int visibleHeight = r.height();
System.out.println(""+visibleHeight);
if (rootViewVisibleHeight == 0) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardShow(0);
}
rootViewVisibleHeight = visibleHeight;
return;
}
//根視圖顯示高度沒有變化曲伊,可以看作軟鍵盤顯示/隱藏狀態(tài)沒有改變
if (rootViewVisibleHeight == visibleHeight) {
return;
}
//根視圖顯示高度變小超過200谆棱,可以看作軟鍵盤顯示了
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
//根視圖顯示高度變大超過200寡喝,可以看作軟鍵盤隱藏了
if (visibleHeight - rootViewVisibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
}
rootViewVisibleHeight = visibleHeight;
}
};
viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener);
}
public void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
}
public interface OnSoftKeyBoardChangeListener {
void keyBoardShow(int height);
void keyBoardHide(int height);
}
}
第二步阶祭,調(diào)用
/**
* 監(jiān)聽軟鍵盤的收起和彈出
*/
protected void backgroundLayoutListener() {
SoftKeyBoardListener softKeyBoardListener = SoftKeyBoardListener.newInstance(activity);
softKeyBoardListener.setOnSoftKeyBoardChangeListener(new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
Log.d(TAG, "鍵盤顯示 高度" + height);
}
@Override
public void keyBoardHide(int height) {
Log.d(TAG, "鍵盤隱藏 高度" + height);
disMissDialogAndCacheInfo();
softKeyBoardListener.clearInstance();
}
});
}