由于项目中需要监听软键盘的弹出 消失,并且需要知道键盘弹出时的高度(为了将画布往上移动,不要挡住输入的UI)
1.我们先定义引擎(下面说的引擎,引擎端都是指core里面)统一接口:
public interface OnKeyboardChangeListener{
public void onKeyboardChange(boolean visible,float keyboardHeight);
}
2.android端实现:
private OnKeyboardChangeListener onKeyboardChangeListener;
private boolean isKeyboardShow;
private float gameKeyboardHeight,screenKeyboardHeight;
private int visibleWidth,visibleHeight;
//这里的context其实是activity
//在平台接口实现类的构造方法里面调用下面代码
final View rootView = this.context.getWindow().getDecorView().getRootView();
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
visibleWidth = rect.width();
visibleHeight = rect.height();
//this shifts min api to 11
rootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
if (!(visibleWidth == rect.width() && visibleHeight == rect.height())) {
visibleWidth = rect.width();
visibleHeight = rect.height();
//这里将键盘高度由原生转换到引擎端与stage高度对应
float gameHeight = 1136;
float hScale = Gdx.graphics.getHeight() / gameHeight;
screenKeyboardHeight = Gdx.graphics.getHeight() - visibleHeight;
gameKeyboardHeight = (int) (screenKeyboardHeight / hScale);
isKeyboardShow = gameKeyboardHeight > 2;
if (onKeyboardChangeListener != null) {
onKeyboardChangeListener.onKeyboardChange(isKeyboardShow, gameKeyboardHeight);
}
}
}
});
3.ios端实现:
private boolean keyBoardVisible;
private OnKeyboardChangeListener onKeyboardChangeListener;
private float keyboardHeight;
//在平台接口的初始化代码中调用下面代码
NSNotificationCenter center = NSNotificationCenter.getDefaultCenter();
center.addObserver(this, Selector.register("keyboardWillShow:"), UIWindow.KeyboardWillShowNotification(), null);
center.addObserver(this, Selector.register("keyboardWillHide:"), UIWindow.KeyboardWillHideNotification(), null);
//对应两个Selector方法实现
@SuppressWarnings("unchecked")
@Method(selector="keyboardWillShow:")
public void keyboardWillShow(NSNotification notification){
keyBoardVisible = true;
//获取键盘的高度
NSDictionary<NSString, NSObject> userInfo = (NSDictionary<NSString, NSObject>) notification.getUserInfo();
NSValue value = (NSValue) userInfo.get(UIKeyboardAnimation.Keys.FrameEnd());
CGRect keyboardRect = NSValueExtensions.getRectValue(value);
double height = keyboardRect.getSize().getHeight();
//这里将键盘高度由原生转换到引擎端与stage高度对应
float gameHeight = 1136;
float hScale = (float) (UIScreen.getMainScreen().getBounds().getHeight() / gameHeight); //1.69
keyboardHeight = (int) (height / hScale);
if (onKeyboardChangeListener != null) {
onKeyboardChangeListener.onKeyboardChange(keyBoardVisible, keyboardHeight);
}
}
@Method(selector="keyboardWillHide:")
public void keyboardWillHide(NSNotification notification){
keyBoardVisible = false;
keyboardHeight = 0;
if (onKeyboardChangeListener != null) {
onKeyboardChangeListener.onKeyboardChange(keyBoardVisible, keyboardHeight);
}
}
//注意在不想监听的时候调用下面代码
NSNotificationCenter center = NSNotificationCenter.getDefaultCenter();
center.removeObserver(this);
//引擎端统一接口
public interface PlatformInterface{
public void setOnKeyboardChangeListener(OnKeyboardChangeListener listener);
public OnKeyboardChangeListener getOnKeyboardChangeListener();
public interface OnKeyboardChangeListener{
public void onKeyboardChange(boolean visible,float keyboardHeight);
}
}
//接下来在引擎端调用
platformInterface.setOnKeyboardChangeListener(new OnKeyboardChangeListener() {
public void onKeyboardChange(boolean visible, float keyboardHeight) {
if (!visible) {
stage.setKeyboardFocus(null);
stage.getRoot().addAction(Actions.moveTo(0, 0, 0.2f));
}else {
stage.getRoot().addAction(Actions.moveTo(0, keyboardHeight, 0.2f));
}
}
});
然后在引擎端设置监听,然后就可以根据键盘高度移动画布。
至此,应该就可以实现键盘弹出时将画布向上推的效果,如果有什么不明白的,欢迎留言。