如何以编程方式关闭Android软键盘?

如何以编程方式关闭Android软键盘?,android,android-softkeyboard,Android,Android Softkeyboard,我目前正在使用以下代码显示软键盘 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN); 在这里,我没有将软键盘与Edittext绑定,因为我使用了上面的代码 现在我想关闭软键盘,所以我目前

我目前正在使用以下代码显示软键盘

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
在这里,我没有将软键盘与Edittext绑定,因为我使用了上面的代码

现在我想关闭软键盘,所以我目前正在使用下面的代码,但它不工作

imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
有人能建议我用什么来关闭软键盘吗


基于下面的回答,我想让你们清楚我并没有使用EditText,我使用的是我想要显示键盘和隐藏键盘的布局。我想将键盘键事件发送到我未使用editText的远程区域bcoz。

您也可以尝试

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

我已经测试过了,这是有效的:

...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

顺便说一下,代码的第二个参数不正确,请查看。

使用此工作代码:

InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

如果需要,可以使用整个类并在以下位置调用KeyboardUtil.hideKeyBoard(上下文)方法:

public class KeyboardUtil
{
public static void hideKeyboard(Activity activity)
    {
        try
        {
            InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
         }
        catch (Exception e)
        {
            // Ignore exceptions if any
                Log.e("KeyBoardUtil", e.toString(), e);
        }
    }
}

user942821关于隐藏它的答案是有效的:

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
但这也可以让我隐藏它:

imm.toggleSoftInput(0, 0);
您可能还想尝试:

imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
当在第一个参数中使用“0”时,有时键盘会在奇怪的情况下在错误的位置切换,我还没有弄清楚如何复制。我仍在测试最后一个示例,但当我发现更多信息时,将进行更新

有关更多信息,请参阅

private void close() {
    this.requestHideSelf(0);
}
此方法非常简单

效果很好

InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);

下面是一个解决方案,它检查键盘是否可见

    public static void hideKeyboard(Activity activity) {
        if (isKeyboardVisible(activity)) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
        }
    }

    public static boolean isKeyboardVisible(Activity activity) {
        ///This method is based on the one described at http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device
        Rect r = new Rect();
        View contentView = activity.findViewById(android.R.id.content);
        contentView.getWindowVisibleDisplayFrame(r);
        int screenHeight = contentView.getRootView().getHeight();

        int keypadHeight = screenHeight - r.bottom;

        return
                (keypadHeight > screenHeight * 0.15);
    }
为了隐藏键盘

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);

这里,“mView”可以是屏幕上可见的任何视图

此代码将键盘隐藏在
自动完成文本视图的
onItemClick
内部

public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) {
     // whatever your code does
     InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
}
public void onItemClick(AdapterView AdapterView、View viewIn、int indexSelected、long arg3){
//不管你的代码做什么
InputMethodManager imm=(InputMethodManager)getSystemService(viewIn.getContext().INPUT_方法_服务);
imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(),0);
}
关闭/隐藏Android软键盘 打开Android软键盘 使用Kotlin扩展插件隐藏键盘 如何显示软键盘 其他相关主题: 简化添加
完成
/
下一步
。。。编辑文本的操作:

删除永远使用
getSystemService
的要求:

打电话到你想打的地方

 hideKeyboard(MainActivity.this);

隐藏键盘的代码与显示键盘的代码相同。是的,但我不知道用什么来代替它。检查线程。这对我不起作用。请阅读上面的编辑/*以将软键盘隐藏在一行中*/
((InputMethodManager)getActivity().getSystemService(Context.INPUT\u METHOD\u SERVICE)).hideSoftInputFromWindow(view.getWindowToken(),0);
一个始终有效并发送标志“HIDE\u NOT\u everys”的解决方案,有趣的是:)这很有效。不过,不需要捕获异常-捕获异常看起来很难看。@Mak然后使用:getCurrentFocus().getWindowToken()好的,
InputMethodManager.SHOW\u FORCED
只是一个表示int值的枚举。因此,它当然可以工作,但这与将其设置为
InputMethodManager.RESULT\u UNCHANGED\u所示的值为0的情况相同。枚举应始终在需要的地方使用。当然,我明白,但这不应该出现在第二个参数中吗?第一个参数用于showFlags
public void toggleSoftInput(int showFlags,int hideFlags)
我认为这更合适:
imm.toggleSoftInput(显示InputMethodManager.RESULT,显示InputMethodManager.RESULT)好观点!:)实际上,在ShowFlags参数中没有支持0值的枚举标志。通过再次阅读文档,我意识到他们指定它可以是0。对不起,我的错,虽然我不认为不提供默认枚举标志是一种好的做法。我也一直在研究它,似乎你应该在这里使用幻数。我添加了一个链接到文档。一个解释会很好。不是在所有情况下都有效。我发现这是一个更大的问题,它首先似乎捕获了所有的边缘情况。嗯,你从哪里得到这个方法?为什么你使用相同的代码行来显示和隐藏?根据和,很难检查键盘是否显示,因此,如果您不知道是否已经显示,切换可能不是一个好主意。这种方法的问题是,如果用户隐藏了键盘,第二次调用将再次显示键盘。谢谢。我想在用户单击登录按钮时隐藏键盘,在这种情况下,此解决方案比
imm.toggleSoftInput(InputMethodManager.SHOW\u FORCED,0)
更有效。因为在横向手机中,键盘覆盖了登录按钮,用户必须手动隐藏键盘。
View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

it's working for me i hope it's work for you..
 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }
// Simple, reuseable call anywhere in code
myView.hideKeyboard()

fun View.hideKeyboard() {
    val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
fun View.showKeyboard() {
    val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
}
  public void hideKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    //Find the currently focused view, so we can grab the correct window token from it.
    View view = activity.getCurrentFocus();
    //If no view currently has focus, create a new one, just so we can grab a window token from it
    if (view == null) {
        view = new View(activity);
    }
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
 hideKeyboard(MainActivity.this);