Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加可以修复键盘高度动态变化导致键盘遮挡输入框的办法 #656

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions library/src/main/java/com/lxj/xpopup/core/BasePopupView.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public void run() {
KeyboardUtils.registerSoftInputChangedListener(getHostWindow(), BasePopupView.this, new KeyboardUtils.OnSoftInputChangedListener() {
@Override
public void onSoftInputChanged(int height) {
onKeyboardHeightChanged(height);
if(popupInfo!=null && popupInfo.xPopupCallback!=null) {
popupInfo.xPopupCallback.onKeyBoardStateChanged(BasePopupView.this,height);
}
Expand Down Expand Up @@ -574,6 +575,11 @@ protected void beforeDismiss(){}
protected void onShow() {
}

/**
* 当键盘高度改变时调用
*/
protected void onKeyboardHeightChanged(int height) {}

@OnLifecycleEvent(value = Lifecycle.Event.ON_DESTROY)
public void onDestroy(){
destroy();
Expand Down Expand Up @@ -656,6 +662,8 @@ public boolean onTouchEvent(MotionEvent event) {
y = 0;
break;
}
} else {
passClickThrough(event);
}
return true;
}
Expand Down
14 changes: 13 additions & 1 deletion library/src/main/java/com/lxj/xpopup/util/XPopupUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ public static int getDecorViewInvisibleHeight(final Window window) {

//监听到的keyboardHeight有一定几率是错误的,比如在同时显示导航栏和弹出输入法的时候,有一定几率会算上导航栏的高度,
//这个不是必现的,暂时无解
private static int correctKeyboardHeight = 0;
//为了方便用户修改键盘高度和获取当前键盘高度
static int correctKeyboardHeight = 0;

public static void moveUpToKeyboard(final int keyboardHeight, final BasePopupView pv) {
if (correctKeyboardHeight == 0) correctKeyboardHeight = keyboardHeight;
Expand All @@ -229,6 +230,17 @@ public void run() {
});
}

//如果变化键盘高度,需要调整弹窗,请使用SimpleCallback,重写onKeyBoardStateChanged,在onKeyBoardStateChanged中调用XPopupUtils.moveUpToKeyboardNow
//或者重写popupView中的onKeyboardHeightChanged
//此处修改correctKeyboardHeight是为了修复打开其他弹窗时键盘高度不对导致遮挡输入框问题
public static void moveUpToKeyboardNow(final int keyboardHeight, final BasePopupView pv) {
correctKeyboardHeight = keyboardHeight;
pv.getPopupContentView().animate().translationY(-keyboardHeight)
.setDuration(200)
.setInterpolator(new OvershootInterpolator(0))
.start();
}

private static void moveUpToKeyboardInternal(int keyboardHeight, BasePopupView pv) {
if (pv.popupInfo == null || !pv.popupInfo.isMoveUpToKeyboard) return;
//暂时忽略PartShadow弹窗和AttachPopupView
Expand Down