简体   繁体   English

android 11如何隐藏软键盘?

[英]How to hide soft keyboard on android 11?

I have an app using to scan barcode, so I want to hide the soft keyboard.我有一个用于扫描条形码的应用程序,所以我想隐藏软键盘。 My app worked fine on devices with android 7, 9... but when I installed it on device run android 11, the keyboard display when I scan.我的应用程序在 android 7、9... 的设备上运行良好,但是当我将它安装在设备上运行 android 11 时,我扫描时键盘显示。

I searched and tried many ways, like this in Manifest:我搜索并尝试了很多方法,例如在 Manifest 中:

android:windowSoftInputMode="stateHidden" android:windowSoftInputMode="stateHidden"

Or this in activity:或者在活动中:

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0); imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);

None of these works.这些都不起作用。 Does anyone have different solutions, please help.有没有人有不同的解决方案,请帮忙。 Thank you.谢谢你。

Here is two different methods for activity and fragment, Hope it might work for you.这是活动和片段的两种不同方法,希望它对您有用。

// Method : Hide Keyboard
    @JvmStatic
    fun hideKeyboard(activity: Activity) {
        val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        var view = activity.currentFocus
        if (view == null) {
            view = View(activity)
        }
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }

    @JvmStatic
    fun hideKeyboardInFragment(context: Context?, view: View?/*Your EditText*/) {
        if (context != null && view != null) {
            val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }

this is worked for me, please check这对我有用,请检查

public static void hideKeyboard(Context context) {
    InputMethodManager inputManager = (InputMethodManager) context
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    // check if no view has focus:
    View v = ((Activity) context).getCurrentFocus();
    if (v == null)
        return;
    inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}

 

you can use this code.您可以使用此代码。 send getActivity() every where...getActivity()发送到任何地方...

public static void hideKeyboard(Activity activity) {
    if (activity != null) {
        View view = activity.getCurrentFocus();

        if (view == null) {
            view = new View(activity);
        }

        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}

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

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