如何在 android 上获得软键盘高度?

how can i get soft keyboard height on android?(如何在 android 上获得软键盘高度?)
本文介绍了如何在 android 上获得软键盘高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天来,我一直在使用 libgdx 开发一个 android 项目.期间出现了一个问题.当软键盘出现时,一些视图会被覆盖,所以我想得到解决这个错误的高度.

我知道在使用android api开发项目时可以设置软输入模式来解决这个问题,libgdx有没有提供什么方法​​?

解决方案

我想分享一个可行的解决方案.

首先,没有办法从 libgdx api 中获取软键盘高度.您必须编写特定于平台的代码.与平台特定代码的接口相当简单——不用担心!从官方 libgdx wiki 阅读本指南.p>

以下代码是原生android代码,应该放在libgdx android gradle模块中:

导入android.graphics.Rect;导入android.view.View;导入 android.view.ViewTreeObserver;导入android.view.Window;导入 com.badlogic.gdx.backends.android.AndroidApplication;/*** 特定于平台的 android 实现的容器.*/公共类 PlatformSpecificAndroidImpl 实现 PlatformSpecificService {私有AndroidApplication androidApplication;私有AndroidGlobalLayoutListener globalLayoutListener;公共 PlatformSpecificAndroidImpl(AndroidApplication androidApplication) {this.androidApplication = androidApplication;}/*** 初始化平台服务.该方法应该从 gdx 应用程序的create()"方法中调用.*/@覆盖公共无效初始化(){globalLayoutListener = new AndroidGlobalLayoutListener(androidApplication);窗口窗口 = androidApplication.getWindow();如果(窗口!= null){查看装饰视图 = window.getDecorView();如果(装饰视图!= null){查看 rootView = decorView.getRootView();if (rootView != null) {ViewTreeObserver viewTreeObserver= rootView.getViewTreeObserver();if (viewTreeObserver != null) {viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener);}}}}}/*** 获取真正可用的窗口高度,如果打开则减去软键盘.* @return 可用窗口高度*/@覆盖公共 int getUsableWindowHeight() {if (globalLayoutListener != null) {返回 globalLayoutListener.getHeight();}返回0;}私有静态类 AndroidGlobalLayoutListener 实现 ViewTreeObserver.OnGlobalLayoutListener {私有AndroidApplication androidApplication;私人 int 高度;私人AndroidGlobalLayoutListener(AndroidApplication androidApplication){this.androidApplication = androidApplication;}@覆盖公共无效 onGlobalLayout() {高度 = 0;窗口窗口 = androidApplication.getWindow();如果(窗口!= null){查看 currentFocus = window.getCurrentFocus();如果(当前焦点!= null){查看 rootView = currentFocus.getRootView();if (rootView != null) {矩形矩形 = 新矩形();rootView.getWindowVisibleDisplayFrame(rect);高度 = rect.bottom;}}}}公共 int getHeight() {返回高度;}}}

现在,您可以在 libgdx 核心模块中通过 PlatformSpecificService 接口调用方法 getUsableWindowHeight().它返回显示高度(以像素为单位)减去软键盘高度.

为什么我在请求时使用了 OnGlobalLayoutListener 而不是直接在 getter 方法中计算高度?好吧,在我看来,这是一个稍微优雅的解决方案.

还有一个问题需要注意:通过Gdx.input.setOnscreenKeyboardVisible(true);打开软键盘涉及到异步通信.如果你碰巧在 setOnscreenKeyboardVisible 之后直接调用 getUsableWindowHeight(),你仍然会得到完整的显示高度,因为键盘还没有真正打开.

i have been developing an android project using libgdx for these days . A question occurred during the period. when soft keyboard' appear , some views will be covered,so i want to get the height for solving this bug.

i know there is a soft input mode can be set to solve this issue when using android api to develop project ,does libgdx provide any way ?

解决方案

I've got a working solution that I'd like to share.

First off, there is no way to get the soft keyboard height out of the libgdx api. You have to write platform specific code. Interfacing with platform specific code is rather simple - don't worry! Read this guide from the official libgdx wiki.

The following code is native android code, that should be placed in the libgdx android gradle module:

import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import com.badlogic.gdx.backends.android.AndroidApplication;

/**
 * Container for platform-specific android implementation.
 */
public class PlatformSpecificAndroidImpl implements PlatformSpecificService {
    private AndroidApplication androidApplication;
    private AndroidGlobalLayoutListener globalLayoutListener;

    public PlatformSpecificAndroidImpl(AndroidApplication androidApplication) {
        this.androidApplication = androidApplication;
    }

    /**
     * Initialize platform services. This method should be called from the gdx applications "create()" method.
     */
    @Override
    public void init() {
        globalLayoutListener = new AndroidGlobalLayoutListener(androidApplication);
        Window window = androidApplication.getWindow();
        if (window != null) {
            View decorView = window.getDecorView();
            if (decorView != null) {
                View rootView = decorView.getRootView();
                if (rootView != null) {
                    ViewTreeObserver viewTreeObserver= rootView.getViewTreeObserver();
                    if (viewTreeObserver != null) {
                        viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener);
                    }
                }
            }
        }
    }

    /**
     * Get the window height that is really usable, subtracting the soft-keyboard if open.
     * @return usable window height
     */
    @Override
    public int getUsableWindowHeight() {
        if (globalLayoutListener != null) {
            return globalLayoutListener.getHeight();
        }
        return 0;
    }

    private static class AndroidGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
        private AndroidApplication androidApplication;
        private int height;

        private AndroidGlobalLayoutListener(AndroidApplication androidApplication) {
            this.androidApplication = androidApplication;
        }

        @Override
        public void onGlobalLayout() {
            height = 0;
            Window window = androidApplication.getWindow();
            if (window != null) {
                View currentFocus = window.getCurrentFocus();
                if (currentFocus != null) {
                    View rootView = currentFocus.getRootView();
                    if (rootView != null) {
                        Rect rect = new Rect();
                        rootView.getWindowVisibleDisplayFrame(rect);
                        height = rect.bottom;
                    }
                }
            }
        }

        public int getHeight() {
            return height;
        }
    }
}

From within the libgdx core module you can now call the method getUsableWindowHeight() via the PlatformSpecificService interface. It returns the display height (in pixels) minus the soft keyboard height.

Why did I use the OnGlobalLayoutListener and not calculate the height directly in the getter method, when it is requested? Well, in my option it is a slightly more elegant solution this way.

There is one more gotcha to be aware of: Opening the soft keyboard via Gdx.input.setOnscreenKeyboardVisible(true); involves asynchronous communication. If you happen to call getUsableWindowHeight() directly after setOnscreenKeyboardVisible, you will still get the full display height, because the keyboard did not yet actually open.

这篇关于如何在 android 上获得软键盘高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)