3

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

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

4

2 回答 2

1

我有一个可行的解决方案,我想分享。

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

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

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;
        }
    }
}

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

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

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

于 2016-09-03T16:14:37.470 回答
0

是的,您可以在 Viewtree Observer 和全局布局侦听器的帮助下,尝试以下提到的步骤

  1. 获取布局的根视图
  2. 获取该根的 Viewtree 观察者,并在此之上添加一个全局布局侦听器。

现在,每当显示软键盘时,android 都会重新调整您的屏幕大小,并且您将收到您的听众的电话。这就是您现在需要做的唯一一件事就是计算您的根视图在重新调整大小后的高度与原始大小之间的差异。如果差异大于 150,则认为这是键盘已充气。

下面是一个示例代码

root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
     public void onGlobalLayout(){
           int heightDiff = root.getRootView().getHeight()- root.getHeight();
           // IF height diff is more then 150, consider keyboard as visible.  
        }
  });
于 2013-09-23T04:15:52.407 回答