Android軟鍵盤之判斷軟鍵盤是否顯示
阿新 • • 發佈:2019-01-25
網上看了不少關於軟鍵盤的顯示操作,這裡自己也做一下記錄。
步驟1.重寫佈局,在onLayout中設定監聽
public class MyKeyBoardLinearLayout extends LinearLayout { private OnSoftKeyboardListener mSoftKeyboardListener; public MyKeyBoardLinearLayout(@NonNull Context context) { super(context); } public MyKeyBoardLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public MyKeyBoardLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public interface OnSoftKeyboardListener{ void onSoftKeyboardChange(); } public void setSoftKeyboardListener(OnSoftKeyboardListener listener){ mSoftKeyboardListener=listener; } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (mSoftKeyboardListener != null) mSoftKeyboardListener.onSoftKeyboardChange(); } }
步驟2. 判斷尺度大小
public boolean isSoftKeyboardShow(View rootView) { int screenHeight; screenHeight = getResources().getDisplayMetrics().heightPixels; Rect rect = new Rect(); rootView.getWindowVisibleDisplayFrame(rect); int visibleBottom = rect.bottom; return visibleBottom < screenHeight * 2 / 3; }
步驟3. 進行監聽
mLlRoot.setSoftKeyboardListener(() -> {
boolean isShow = isSoftKeyboardShow(mLlRoot);
if (isShow) {
UIUtils.showToast("展示");
} else {
UIUtils.showToast("隱藏");
}
});
方式二:(也是很easy的原理差不多,優勢是用不著重新寫佈局)
private void setViewObserver() {
ViewTreeObserver observer = mEtTest.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (KeyboardHelper.isKeyboardVisible(TestActivity.this)){
UIUtils.showToast("可見");
}else {
UIUtils.showToast("隱藏");
}
}
});
}
/**
* Determine if keyboard is visible
*
* @param activity Activity
* @return Whether keyboard is visible or not
*/
public static boolean isKeyboardVisible(Activity activity) {
Rect r = new Rect();
View activityRoot = ViewHelper.getActivityRoot(activity);
int visibleThreshold =
Math.round(DisplayHelper.dp2px(activity, KEYBOARD_VISIBLE_THRESHOLD_DP));
// KEYBOARD_VISIBLE_THRESHOLD_DP = 100;
activityRoot.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRoot.getRootView().getHeight() - r.height();
return heightDiff > visibleThreshold;
}
我推薦使用的是方式二來進行處理。簡易便捷,而且不用麻煩的寫佈局,是的呢。
點選空白處實現軟鍵盤的隱藏效果
/**
* 點選空白處實現 隱藏軟鍵盤的功能(點選的是EditText 那麼就還是顯示 這沒問題的) ----
* @param view
* @param windowToken
*/
public static void hideKeyboard(final View view,IBinder windowToken) {
if(windowToken == null){
return;
}
InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getApplicationContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager == null){
return;
}
boolean active = inputMethodManager.isActive();
if (active) {
inputMethodManager.hideSoftInputFromWindow(windowToken, 0);
}
}
// 進行方法呼叫的方式如下:
mContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getCurrentFocus() != null) {
KeyboardHelper.hideKeyboard(mBtnNext, getCurrentFocus().getWindowToken());
}
}
});