如何在安卓系统中检查软键盘的可见性?

559

我需要做一件非常简单的事情——找出软键盘是否显示。这在安卓系统中可行吗?


9
尽管Reuben Scratton的回答很好,但在平板电脑上似乎有问题。我将检查diff> 128替换为diff> screenHeight/3。 - kingston
2
Reuben Scratton的回答很好,但我需要KaChi的调整才能真正使用它。 - Cullan
2
为什么谷歌不为所有的键盘应用程序制作一个通用的内置方法? - 林果皞
5
我很在意这不是一个系统功能的事实。 - longi
1
这个API仍然缺失了10年,真是太疯狂了。非常高兴我已经远离Android了。 - Reuben Scratton
显示剩余7条评论
45个回答

0

或许这会对你有所帮助:

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

2
虽然这段代码片段可能解决了问题,但包括解释真的有助于提高您的帖子质量。请记住,您正在为未来的读者回答问题,而这些人可能不知道您的代码建议原因。请尽量不要在代码中添加过多的解释性注释,这会降低代码和解释的可读性! - 4b0

0

我知道这是一篇旧帖子,但我认为这是我所知道的最简单的方法,我的测试设备是Nexus 5。我还没有在其他设备上尝试过它。希望如果其他人发现我的代码不好,他们会分享自己的方法 :)

public static boolean isKeyboardShown(Context context, View view) {
        if (context == null || view == null) {
            return false;
        }
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        return imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
}

imm.hideSoftInputFromWindow 返回布尔值。

谢谢,


0
if (keyopen())
{
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0);            
}

上面的函数是我用来检查键盘是否可见的。如果是,我就关闭它。
下面展示了所需的两种方法。
首先,在onCreate中定义可操作窗口的高度。
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//  add to onCreate method
    Rect rectgle= new Rect();
    Window window= getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    sheight= rectgle.bottom;
//

} 

然后,添加一个布尔方法,在该实例中获取窗口高度。 如果它不匹配原始值(假设您在途中没有更改它...),则键盘已打开。
public boolean keyopen()
{
    Rect rectgle= new Rect();
    Window window= getWindow();
    window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
    int curheight= rectgle.bottom;

    if (curheight!=sheight)
    {
        return true;
    }
    else
    {
        return false;
    }
}

弗罗茨!


0

参考@TacB0sS的this答案,我已经用Kotlin开发了一个类。希望这对你有所帮助。如果需要改进,请告诉我。

class KeyboardVisibilityObserver(val layRootContainer: View?, val keyboardVisibilityListener: KeyboardVisibilityListener?) {
    var isKeyboardOpen = false
        private set

    private var keyBoardObserver = object : ViewTreeObserver.OnGlobalLayoutListener {

        private val DefaultKeyboardDP = 100

        // Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff
        private val EstimatedKeyboardDP = DefaultKeyboardDP + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 48 else 0

        private val r = Rect()

        override fun onGlobalLayout() {
            if (layRootContainer != null) {
                // Convert the dp to pixels.
                val estimatedKeyboardHeight = TypedValue
                        .applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP.toFloat(), layRootContainer.resources.displayMetrics).toInt()

                // Conclude whether the keyboard is shown or not.
                layRootContainer.getWindowVisibleDisplayFrame(r)
                val heightDiff = layRootContainer.rootView.height - (r.bottom - r.top)
                val isShown = heightDiff >= estimatedKeyboardHeight

                if (isShown == isKeyboardOpen) {
                    //  Log.d("Keyboard state", "Ignoring global layout change...");
                    return
                }

                isKeyboardOpen = isShown

                keyboardVisibilityListener?.onKeyboardVisibilityChanged(isKeyboardOpen)
            }
        }
    }

    init {
        layRootContainer?.viewTreeObserver?.addOnGlobalLayoutListener(keyBoardObserver)
    }

    // call this in onDestroy
    fun removeObserver(){
        layRootContainer?.viewTreeObserver?.removeOnGlobalLayoutListener(keyBoardObserver)
    }

    interface KeyboardVisibilityListener {
        fun onKeyboardVisibilityChanged(isKeyboardOpen: Boolean)
    }
}

-3

InputMethodManager类包含有关软键盘的信息。您可以通过以下方式从活动中获取它:

((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))

你可以尝试一下,看看它能告诉你什么。你可以使用它来显示或隐藏软输入...


6
我之前查看了InputMethodManager,不幸的是我找不到任何能告诉我键盘是否打开或隐藏的信息。 - fhucho

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