Android-在Android 8上隐藏键盘 [英] Android - Hide keyboard on Android 8

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

问题描述

我在隐藏Android 8上的键盘时遇到了麻烦. 我以前用过,它适用于较旧的android:

I'm having troubles with hiding the keyboard on Android 8. I used this before and it worked for the older androids:

    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 just ignores it and shows the keyboard anyway. Probably making the input field unfocusable would help, but I really need it to be focusable, so this is not an option.

推荐答案

您可以使用toggleSoftInput代替hideSoftInputFromWindow.

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

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

works for Android 8 on the emulator at least

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

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