简体   繁体   English

在Android Oreo上隐藏SoftKeyBoard

[英]Hide SoftKeyBoard on Android Oreo

i'm trying to prevent the keyboard from showing up after calling requestFocus() on a SearchView but there is no solution to fix that on Android 8. 我试图防止在SearchView上调用requestFocus()后出现键盘,但是没有解决方案可以在Android 8上修复该问题。

i tried : 我试过了 :

/*1/ / * 1 /

 android:windowSoftInputMode="stateAlwaysHidden"

/*2/ / * 2 /

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

/*3/ / * 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/ / * 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() worked but the softkeyboard show up for nearly 0.5s and disappear. onGlobalLayout()起作用了,但是软键盘显示了将近0.5s并消失了。

Any help to make that softkeyboard hidden even after calling requestFocus() ?? 即使在调用requestFocus()之后,也可以使该软键盘隐藏起来的任何帮助?

finally i solved the problem, i just used that function bellow to intercept FocusChanges and hide the softKeyboard after 100 (enought time to intercept the softkeyboard) 终于我解决了这个问题,我只是使用了下面的那个函数来拦截FocusChanges并在100之后隐藏softKeyboard(需要时间来拦截softkeyboard)

      @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.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM