我的布局中有一个EditText和一个Button。

在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?


当前回答

我还有一个隐藏键盘的解决方案:

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

这里在showFlag位置传递HIDE_IMPLICIT_ONLY,在hiddenFlag位置传0。它将强制关闭软键盘。

其他回答

通过将Generic方法添加到Utility或Helper类。通过调用自己,您可以隐藏和显示键盘

fun AppCompatActivity.hideKeyboard() {
            val view = this.currentFocus
            if (view != null) {
                val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(view.windowToken, 0)
            }
           window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

        }


 fun AppCompatActivity.showKeyboard() {
            val view = this.currentFocus
            if (view != null) {
                val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(view.windowToken, 0)
            }                     window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

        }

Android 11中有一个名为WindowInsetsController的新API,应用程序可以从任何视图访问控制器,通过它我们可以使用hide()和show()方法

val controller = view.windowInsetsController

// Show the keyboard (IME)
controller.show(Type.ime())

// Hide the keyboard
controller.hide(Type.ime())

看见https://developer.android.com/reference/android/view/WindowInsetsController

您还可以研究在EditText上使用setImeOption。

我只是遇到了一个非常模拟的情况,我的布局包含一个EditText和一个搜索按钮。当我发现我可以在editText中将time选项设置为“actionSearch”时,我意识到我甚至不再需要搜索按钮。软键盘(在此模式下)有一个搜索图标,可用于启动搜索(键盘会自动关闭)。

我已经尝试了所有的解决方案,但没有一个解决方案适合我,所以我找到了我的解决方案:您应该有如下布尔变量:

public static isKeyboardShowing = false;
InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
            .getSystemService(Context.INPUT_METHOD_SERVICE);

在触摸屏事件中,您可以拨打:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(keyboardShowing==true){
        inputMethodManager.toggleSoftInput(InputMethodManager.RESULT_UNCHANGED_HIDDEN, 0);
        keyboardShowing = false;
    }
    return super.onTouchEvent(event);
}

在EditText中的任何位置:

yourEditText.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            yourClass.keyboardShowing = true;

        }
    });
public final class UtilsKeyboard {
/**
 * Sets the cursor at the end of this edit text and shows a keyboard
 */
public static void focusKeyboard(@NonNull EditText editText) {
    editText.requestFocus();
    editText.setSelection(editText.getText().length());
    showKeyboard(editText);
}

private static void showKeyboard(@NonNull View view) {
    final InputMethodManager manager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    if (manager != null) {
        manager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }
}

public static boolean hideKeyboard(@NonNull Window window) {
    View view = window.getCurrentFocus();
    return hideKeyboard(window, view);
}

private static boolean hideKeyboard(@NonNull Window window, @Nullable View view) {
    if (view == null) {
        return false;
    }
    InputMethodManager inputMethodManager = (InputMethodManager) window.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    if (inputMethodManager != null) {
        return inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    return false;
}

public static boolean hideKeyboard(@Nullable View view) {
    if (view == null) {
        return false;
    }
    InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    if (inputMethodManager != null) {
        return inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    return false;
}

/**
 * This will hide the keyboard and unfocus any focused view.
 */
public static void unfocusKeyboard(@NonNull Window window) {
    // When showing the bottom menu, make sure the keyboard isn't and that no view has focus
    hideKeyboard(window);
    final View focused = window.getCurrentFocus();
    if (focused != null) focused.clearFocus();
}}