如何通过编程方式关闭Android Soft KeyBoard?

时间:2012-01-09 07:12:51

标签: android android-softkeyboard

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

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);

在这里我没有使用Edittext绑定软键盘,因为我使用了上面的代码。

现在我想关闭SoftKeyboard,所以我目前正在使用以下代码,但它无法正常工作。

imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);

有人可以建议我使用什么来关闭softKeyboard吗?


基于下面的答案我想让你清楚我没有使用EditText,我使用Layout来显示键盘和隐藏键盘。我想将键盘键事件发送到我没有使用editText的远程区域bcoz。

15 个答案:

答案 0 :(得分:92)

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

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

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

顺便提一下,您的代码的第二个参数不正确,请查看here

答案 1 :(得分:39)

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

答案 2 :(得分:31)

使用此工作代码:

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

答案 3 :(得分:9)

如果需要,您可以使用整个类并在任何地方调用KeyboardUtil.hideKeyBoard(context)方法:

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);
        }
    }
}

答案 4 :(得分:3)

关闭/隐藏Android软键盘

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

打开Android软键盘

 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }

答案 5 :(得分:2)

user942821隐藏它的答案有效:

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

但是这对我隐藏它也很有用:

imm.toggleSoftInput(0, 0);

您可能还想尝试:

imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

当在第一个参数中使用'0'时,键盘会在奇怪的情况下在错误的位置切换,我还无法弄清楚如何复制。我还在测试最后一个例子,但是当我发现更多时会更新。

有关详细信息,请参阅toggleSoftInput documentation page

答案 6 :(得分:2)

这很好用

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

答案 7 :(得分:0)

您也可以尝试

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

答案 8 :(得分: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);
    }

答案 9 :(得分:0)

InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

答案 10 :(得分:0)

隐藏键盘,

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

此处,“mView”可以是在屏幕上可见的任何视图

答案 11 :(得分:0)

此代码隐藏了onItemClick

AutoCompleteTextView内部的键盘
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);
}

答案 12 :(得分:0)

使用Kotlin扩展程序隐藏键盘

// 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)
}

其他相关主题:

简化向编辑文本中添加Done / Next ...操作:read this post

删除永远使用getSystemService的要求:Splitties Library

答案 13 :(得分:0)

  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);

答案 14 :(得分:-2)

private void close() {
    this.requestHideSelf(0);
}

这种方法非常简单