简体   繁体   English

在 Android 中以编程方式检测“显示虚拟键盘”设置

[英]Detect “Show virtual keyboard” setting programatically in Android

There's a setting in Android called "Show virtual keyboard", which in my phone is located in: Settings > Languages & input > Physical keyboard . Android 中有一个名为“显示虚拟键盘”的设置,在我的手机中位于: Settings > Languages & input > Physical keyboard It controls whether to keep the virtual keyboard on the screen while a physical keyboard is active.它控制是否在物理键盘处于活动状态时将虚拟键盘保留在屏幕上。

Is there a way of programatically querying whether it is enabled or disabled?有没有办法以编程方式查询它是启用还是禁用? I can't find it in the Settings.System list in Android developers .在 Android 开发人员的 Settings.System 列表中找不到它。

I don't know exactly if you can query that information but if it helps you, there is a way to programatically detect if the keyboard is opened or not.我不确切知道您是否可以查询该信息,但如果它对您有帮助,有一种方法可以以编程方式检测键盘是否打开。

private boolean isKeyboardShowing = false;

private void detectKeyboard() {
    rootView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {

                    Rect r = new Rect();
                    rootView.getWindowVisibleDisplayFrame(r);
                    int screenHeight = rootView.getRootView().getHeight();

                    int keypadHeight = screenHeight - r.bottom;

                    if (keypadHeight > screenHeight * 0.15) {
                        // keyboard is opened
                        if (!isKeyboardShowing) {
                            isKeyboardShowing = true;

                        }
                    }
                    else {
                        // keyboard is closed
                        if (isKeyboardShowing) {
                            isKeyboardShowing = false;
               
                        }
                    }
                }
            });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM