问题
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 ?
回答1:
Yes you can, with the help of Viewtree Observer and global layout listener, just try below mentioned steps
- Get the root view of your layout
- get the Viewtree observer for this root, and add a global layout listener on top of this.
now whenever soft keyboard is displayed android will re-size your screen and you will receive call on your listener. That's it only thing you now need to do is calculate difference between height which your root view has after re-size and original size. If difference is more then 150 consider this as a keyboard has been inflated.
Below is a sample code
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.
}
});
回答2:
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.
来源:https://stackoverflow.com/questions/18951637/how-can-i-get-soft-keyboard-height-on-android