問題

我需要做一件非常簡單的事情 – 找出是否顯示了軟體鍵盤.這可能在Android中嗎?

  最佳答案

新 ANSWER 2012年1月25日增加

自從寫下面的答案以來,有人會讓我瞭解 ViewTreeObserver 和朋友的存在,API自版本1以來一直隱藏在SDK中.

不需要自定義Layout型別,更簡單的解決方案是給活動的根檢視一個已知的ID,比如@+id/activityRoot,將GlobalLayoutListener繫結到ViewTreeObserver,並從那裡計算活動的檢視根和視窗大小之間的大小差異:

 final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
        if (heightDiff > dpToPx(this, 200)) { // if more than 200 dp, it's probably a keyboard...
            // ... do something here
        }
     }
});
 

使用一個實用程式,如:

 public static float dpToPx(Context context, float valueInDp) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
}
 

簡單!

注: 您的應用程式必須在Android Manifest android:windowSoftInputMode="adjustResize"中設定此標誌,否則上面的解決方案將不起作用.

原件

是的,這是可能的,但它比應該要難得多。

如果我需要關心鍵盤出現和消失時(這經常),那麼我所做的是將我的toplevel佈局類自定義為覆蓋onMeasure()的類.基本邏輯是,如果佈局發現自己的填充大大低於視窗的總面積,那麼軟鍵盤可能會顯示.

 import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/*
 * LinearLayoutThatDetectsSoftKeyboard - a variant of LinearLayout that can detect when 
 * the soft keyboard is shown and hidden (something Android can't tell you, weirdly). 
 */

public class LinearLayoutThatDetectsSoftKeyboard extends LinearLayout {

    public LinearLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public interface Listener {
        public void onSoftKeyboardShown(boolean isShowing);
    }
    private Listener listener;
    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        Activity activity = (Activity)getContext();
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.top;
        int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
        int diff = (screenHeight - statusBarHeight) - height;
        if (listener != null) {
            listener.onSoftKeyboardShown(diff>128); // assume all soft keyboards are at least 128 pixels high
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);       
    }

    }
 

然後在Activity類中...

 public class MyActivity extends Activity implements LinearLayoutThatDetectsSoftKeyboard.Listener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        LinearLayoutThatDetectsSoftKeyboard mainLayout = (LinearLayoutThatDetectsSoftKeyboard)findViewById(R.id.main);
        mainLayout.setListener(this);
        ...
    }


    @Override
    public void onSoftKeyboardShown(boolean isShowing) {
        // do whatever you need to do here
    }

    ...
}
 

  相同標籤的其他問題

androidvisibilityandroid-softkeyboard