Android - 在Android 8上隐藏键盘

17

我在Android 8上隐藏键盘遇到了麻烦。 我之前使用过这个方法,对于旧版本的Android有效:

    val view = activity.currentFocus
    if (view != null) {
        val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }

Android 8会忽略它并仍然显示键盘。也许使输入字段无法聚焦会有所帮助,但我确实需要它可以聚焦,所以这不是一个选项。

Android 8会忽略它并仍然显示键盘。也许使输入字段无法聚焦会有所帮助,但我确实需要它可以聚焦,所以这不是一个选项。


返回的布尔值是什么?第三个参数可以是“ResultReceiver”,如果使用它,它会接收到什么数字? - Eugen Pechanec
imm.requestHideSelf 能用吗? - Lovis
@EugenPechanec 进入方法(视图不为空) - user9519356
@Lovis 没有这个函数。 - user9519356
抱歉,你是对的,它在InputMethodService中。而且你不应该获取那个。 - Lovis
8个回答

5

你可以使用toggleSoftInput代替hideSoftInputFromWindow

val imm: InputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
if (imm.isActive)
     imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)

至少在模拟器上适用于Android 8


谢谢! 它不是完美的,但比以前好多了 :D 现在它的功能是,键盘显示约 0.5 秒,然后隐藏。 - user9519356

3
@EarlOfEgo的解决方案在较旧的Android版本上可能会出现问题。以下是理想的解决方案,适用于所有(至少几乎所有)Android版本:
protected fun hideKeyboard() {
    val view = activity.currentFocus
    if(android.os.Build.VERSION.SDK_INT >= 26) {
        val imm: InputMethodManager = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        view?.post({
            imm.hideSoftInputFromWindow(activity.currentFocus.windowToken, 0)
            imm.hideSoftInputFromInputMethod(activity.currentFocus.windowToken, 0)
        })
    } else {
        if (view != null) {
            val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.hideSoftInputFromWindow(view.windowToken, 0)
            imm.hideSoftInputFromInputMethod(view.windowToken, 0)
        }
    }
}

1

这里有两个静态函数用于隐藏键盘,根据您的情况选择使用哪一个。我在Android Oreo上进行了测试,它可以正常工作。

object UIHelper {

            fun hideSoftKeyboard(activity: Activity?) {
                if (activity != null) {
                    val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    if (activity.currentFocus != null && inputManager != null) {
                        inputManager.hideSoftInputFromWindow(activity.currentFocus!!.windowToken, 0)
                        inputManager.hideSoftInputFromInputMethod(activity.currentFocus!!.windowToken, 0)
                    }
                }
            }

            fun hideSoftKeyboard(view: View?) {
                if (view != null) {
                    val inputManager = view!!.getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    inputManager?.hideSoftInputFromWindow(view!!.getWindowToken(), 0)
                }
            }

            fun showKeyboard(activityContext: Context, editText: EditText) {
                editText.requestFocus()
                Handler().postDelayed(Runnable {
                    val inputMethodManager = activityContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
                }, 250)
            }
        }

使用示例:

  1. UIHelper.hideSoftKeyboard(this)
  2. UIHelper.hideSoftKeyboard(passwordField)

显示:

    UIHelper.showKeyboard(this, passwordField)

0
我最近也遇到了同样的问题,我的解决方法是从(在我的情况下)片段中提供根视图作为窗口令牌,而不是活动的当前焦点。

这样,键盘就会消失,EditText中的焦点也会保持。

在运行Android 8.1的Pixel 2 XL上进行了测试:

/**
 * Hides the Soft Keyboard on demand
 *
 * @param activity the activity from which to get the IMM
 * @param view the view from which to provide a windowToken
 */
fun hideSoftKeyboard(activity: Activity, view: View?) {
    val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(view?.windowToken, 0)
}

0

我曾经遇到过类似的问题,我是这样解决的:

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        window.setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

    }
}

这不是最优雅的解决方案。但在我的情况下是可以接受的。

0
请使用我创建的这个方法。
    public static void showHideInput(Context context,boolean visibility, View view){

    if (view != null) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (visibility) imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
        else imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

0

在可运行的内部隐藏键盘,使用您视图的post方法进行调用:

view.post(() -> {
   hideKeyboard(view);
}

0

尝试使用以下方法拦截焦点事件并隐藏软键盘:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    if (hasFocus) {
        View lFocused = getCurrentFocus();
        if (lFocused != null)
            lFocused.postDelayed(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager lInputManager = (InputMethodManager) pContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    lInputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                }
            }, 100);//Modified to 100ms to intercept SoftKeyBoard on Android 8 (Oreo) and hide it.
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接