如何在安卓系统中获取软键盘的高度?

3

我最近几天一直在使用libgdx开发一个Android项目。在开发过程中,遇到了一个问题:当软键盘弹出时,会覆盖一些视图,因此我想获取高度来解决这个问题。

我知道在使用Android API开发项目时可以设置软输入模式来解决这个问题,但是Libgdx有没有提供任何方式来解决呢?


以下是在以下线程中的解决方法: https://dev59.com/G2kw5IYBdhLWcg3w2-Mk#9605260 - JohnyTex
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核心模块内部,您现在可以通过PlatformSpecificService接口调用方法getUsableWindowHeight()。它返回显示高度(以像素为单位),减去软键盘高度。
为什么我使用OnGlobalLayoutListener而不是直接在getter方法中计算高度呢?嗯,在我看来,这是一个稍微更加优雅的解决方案。
还有一个要注意的问题: 通过Gdx.input.setOnscreenKeyboardVisible(true);打开软键盘涉及异步通信。如果您恰好在setOnscreenKeyboardVisible之后直接调用getUsableWindowHeight(),则仍将获得完整的显示高度,因为键盘尚未实际打开。

0

是的,你可以通过Viewtree Observer和全局布局监听器的帮助来实现,只需尝试以下步骤:

  1. 获取您布局的根视图
  2. 获取此根视图的Viewtree observer,并在其上添加全局布局监听器。

现在,每当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.  
        }
  });

谢谢,我已经尝试了你上面提到的方法,但似乎没有任何改变。当软键盘显示时,onGlobalLayout()函数没有被调用。 - mogutou
这并不提供键盘的高度。 - what is sleep

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