在 Android Oreo 上隐藏 SoftKeyBoard

Hide SoftKeyBoard on Android Oreo

我试图在 SearchView 上调用 requestFocus() 后阻止键盘显示,但在 Android 8.

上没有修复该问题的解决方案

我试过了:

/*1/

 android:windowSoftInputMode="stateAlwaysHidden"

/*2/

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

/*3/

InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (activity.getCurrentFocus() != null && activity.getCurrentFocus().getWindowToken() != null)
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);

/*4/

 final View activityRootView = findViewById(R.id.rootLayout);
                activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {
                        int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
                        if (heightDiff > dpToPx(Act_StoreOrder.this, 200)) { // if more than 200 dp, it's probably a keyboard...
                            Log.i("<<<Clavier","Clavier Showed Up");
                            //hide here 
                        }
                    }
                });

onGlobalLayout() 有效,但软键盘显示将近 0.5 秒后消失。

即使在调用 requestFocus() 之后也能隐藏该软键盘的任何帮助??

最后我解决了这个问题,我只是使用下面的函数来拦截 FocusChanges 并在 100 之后隐藏软键盘(足够的时间来拦截软键盘)

      @Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    if (hasFocus) {
        View lFocused = getCurrentFocus();
        if (lFocused != null)
            lFocused.postDelayed(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager lInputManager = (InputMethodManager) pContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    lInputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                }
            }, 100);//Modified to 100ms to intercept SoftKeyBoard on Android 8 (Oreo) and hide it.
    }
}