这几天我一直在开发一个使用libgdx的android项目。期间发生了一个问题。当软键盘出现时,一些视图将被覆盖,所以我想获得解决这个错误的高度。我如何才能在android上获得软键盘高度?
我知道有一个软输入模式可以设置来解决这个问题,使用android api开发项目时,请问libgdx提供什么方法?
这几天我一直在开发一个使用libgdx的android项目。期间发生了一个问题。当软键盘出现时,一些视图将被覆盖,所以我想获得解决这个错误的高度。我如何才能在android上获得软键盘高度?
我知道有一个软输入模式可以设置来解决这个问题,使用android api开发项目时,请问libgdx提供什么方法?
当然可以,用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.
}
});
感谢名单,我已经试过这种方式,你提到的以上,但似乎没有任何改变。当软键盘显示时,函数onGlobalLayout()不会被调用 – mogutou
这不会给你键盘的高度。 –
它也不适用于我。 –
我有,我想分享一个有效的解决方案。
首先,没有办法让软键盘高度离开libgdx api。你必须编写平台特定的代码。与特定于平台的代码进行接口相当简单 - 不用担心! Read this guide from the official 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()
,则仍然会获得完整的显示高度,因为键盘尚未实际打开。
在以下螺纹的一种解决方法: http://stackoverflow.com/questions/9604963/how-do-libgdx-detect-keyboard-presence/9605260#9605260 – JohnyTex