Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 監聽軟鍵盤彈出與收起的可行做法

編輯:關於 Android 程式設計

需要知道軟鍵盤此刻是否顯示,聽起來簡單,實作起來卻意外棘手。以下記錄幾種嘗試過的方案,以及最後採用的解法。

方案一:檢查 softInputMode(不建議)

網路上常見的寫法:

 getWindow().getAttributes().softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED

實測後發現它只對系統內建鍵盤有效,裝了第三方輸入法就完全抓不到。

方案二:InputMethodManager.isActive()(仍有局限)

 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 boolean isOpen = imm.isActive(); // true 代表輸入法處於啟用狀態

這個方法回傳的是「輸入法是否啟用」,並無法即時反映鍵盤的顯示與隱藏,因此同樣滿足不了需求。

方案三:監聽版面高度變化(採用)

最可靠的做法是監聽根 View 的版面變化,用可見區域高度的差值推斷鍵盤狀態:

 import android.app.Activity;
 import android.content.Context;
 import android.graphics.Rect;
 import android.os.Build;
 import android.util.Log;
 import android.util.TypedValue;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver;

 /** 軟鍵盤的監聽 */
 public class KeyBoardShowListener {
 private Context ctx;

 public KeyBoardShowListener(Context ctx) {
 this.ctx = ctx;
 }

 OnKeyboardVisibilityListener keyboardListener;

 public OnKeyboardVisibilityListener getKeyboardListener() {
 return keyboardListener;
 }

 public interface OnKeyboardVisibilityListener {
 void onVisibilityChanged(boolean visible);
 }

 public void setKeyboardListener(final OnKeyboardVisibilityListener listener, Activity activity) {
 final View activityRootView = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
 activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
 private boolean wasOpened;
 private final int DefaultKeyboardDP = 100;
 // Lollipop 之後根節點會把按鈕列算進來,因此補上 48dp
 private final int EstimatedKeyboardDP = DefaultKeyboardDP
 + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);
 private final Rect r = new Rect();

 @Override
 public void onGlobalLayout() {
 int estimatedKeyboardHeight = (int) TypedValue.applyDimension(
 TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP,
 activityRootView.getResources().getDisplayMetrics());

 activityRootView.getWindowVisibleDisplayFrame(r);
 int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
 boolean isShown = heightDiff >= estimatedKeyboardHeight;

 if (isShown == wasOpened) {
 Log.e("Keyboard state", "Ignoring global layout change...");
 return;
 }
 wasOpened = isShown;
 listener.onVisibilityChanged(isShown);
 }
 });
 }
 }

wasOpened 這個旗標很重要,它能過濾掉無意義的重複回呼。使用方式:

 new KeyBoardShowListener(Activity.this).setKeyboardListener(
 new KeyBoardShowListener.OnKeyboardVisibilityListener() {
 @Override
 public void onVisibilityChanged(boolean visible) {
 if (visible) {
 // 軟鍵盤已彈出
 } else {
 // 軟鍵盤已收起
 }
 }
 }, Activity.this);

常見的鍵盤操作

讓 EditText 取得焦點並彈出鍵盤

 edit.setFocusable(true);
 edit.setFocusableInTouchMode(true);
 edit.requestFocus();
 InputMethodManager inputManager = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
 inputManager.showSoftInput(edit, 0);

收起鍵盤並清除焦點

 editMsgView.setText("");
 editMsgView.clearFocus();
 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);

永遠不彈出鍵盤

 EditText edit = (EditText) findViewById(R.id.edit);
 edit.setInputType(InputType.TYPE_NULL);

或者在需要時主動收起:

 InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
 if (imm.isActive()) {
 imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
 }
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved