在任何地方隐藏软键盘 [英] Hide Soft Keyboard from anywhere

查看:85
本文介绍了在任何地方隐藏软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

隐藏软键盘很痛苦. 我使用一些基于具有焦点的EditText的方法,但是在我当前的应用中,键盘会在加载新片段的某个位置不断弹出.

Hiding the soft keyboard is pain. I use some methods based on having an EditText which gets focus, but in my current app the keyboard keeps popping up at some point where a new fragment is loaded.

我的帮助器类中有一个方法,但是对我不起作用:

I have a method in my helper class, but it does not work for me:

 //Hide keyboard
    public static void hideSoftKeyboard(Activity activity) {
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

我想要的是一种帮助方法,我可以从任何地方调用以隐藏软键盘.这有可能吗?还是我总是需要找到重点突出的EditText?

What I would love is a helper method, I could call from anywhere to hide the soft keyboard. Is this possible, or do I always need the find the EditText which is focused?

推荐答案

执行类似的操作可传递该活动的任何edittext id.它将对该活动有效

Do something like this pass any edittext id of that activity..it will work for that activty

public static void hideSoftKeyboard(Activity activity, EditText editText) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(
                Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }

这篇关于在任何地方隐藏软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆