Androidでソフトウェアキーボードの表示を確認するにはどうすればいいですか? 質問する

Androidでソフトウェアキーボードの表示を確認するにはどうすればいいですか? 質問する

非常に簡単なこと、つまりソフトウェア キーボードが表示されているかどうかを確認する必要があります。これは Android で可能ですか?

ベストアンサー1

新しい回答が 2012年1月25日に追加されました

以下の回答を書いてから、ある人が私に存在を教えてくれビューツリーオブザーバーそして、バージョン 1 以来 SDK に潜んでいる API もいくつかあります。

カスタム レイアウト タイプを必要とするのではなく、アクティビティのルート ビューに既知の ID を指定して、@+id/activityRootGlobalLayoutListener を 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 マニフェストでこのフラグを設定する必要がありますandroid:windowSoftInputMode="adjustResize"。そうしないと、上記のソリューションは機能しません。

オリジナルの回答

はい、可能ですが、本来あるべきよりもはるかに困難です。

キーボードがいつ表示され、消えるかを気にする必要がある場合 (これはかなり頻繁に発生します)、トップレベルのレイアウト クラスを をオーバーライドするようにカスタマイズします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);       
    }

    }

次に、アクティビティ クラスで...

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
    }

    ...
}

おすすめ記事