Android 软键盘导致UI被遮挡问题

Android 软键盘导致UI被遮挡问题


Android 软键盘导致UI被遮挡问题


情况1:LinearLayout+android:fitsSystemWindows="false"(根目录) +android:windowSoftInputMode="stateHidden|adjustResize"(mainfast)
// 上面设置情况 软键盘弹出页面不会动 自己来监听软键盘高度 然后设置父布局的paddingBottom为软键盘高度
KeybordS.observeSoftKeyboard(getWContext().get(), new KeybordS.OnSoftKeyboardChangeListener() {
@Override
public void onSoftKeyBoardChange(int softKeybardHeight, boolean visible) {
if (!visible) softKeybardHeightInClose = softKeybardHeight;
JLog.e(visible + "--" + softKeybardHeight);
if (visible) {
llMall.setPadding(0, 0, 0, softKeybardHeight - softKeybardHeightInClose);
} else {
llMall.setPadding(0, 0, 0, 0);
}
}
});

情况二:android:fitsSystemWindows="true" +android:windowSoftInputMode="stateHidden|adjustResize"+ScrollView
并且外部套有一层ScrollView
// 上面设置情况 软键盘弹出页面会进行绘制下方自己加上一个边距 让原来最下面的控件露出来
但是因为fitsSystemWindows=true 重新绘制 然后被滚动到了焦点位置 所以焦点下方的控件又被遮挡了
//所以我们直接滚动打最底部就可以
// 输入框UI重新计算可视区域 方便把发布按钮顶上去
KeybordS.observeSoftKeyboard(this, new KeybordS.OnSoftKeyboardChangeListener() {
@Override
public void onSoftKeyBoardChange(int softKeybardHeight, boolean visible) {
if (visible) {
scrollview.smoothScrollTo(0, scrollview.getChildAt(0).getHeight());
} else {
cancelEtFouse();
}
}
});




public class KeybordS {

/**
* 打开软键盘
*
* @param mEditText
* @param mContext
*/
public static void openKeybord(EditText mEditText, Context mContext) {
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
}

/**
* 关闭软键盘
*
*/
public static void closeKeybord(EditText mEditText, Context mContext) {
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}

/**
* 判断当前软键盘是否打开
*
* @param activity
* @return
*/
public static boolean isSoftInputShow(Activity activity) {

// 虚拟键盘隐藏 判断view是否为空
View view = activity.getWindow().peekDecorView();
if (view != null) {
// 隐藏虚拟键盘
InputMethodManager inputmanger = (InputMethodManager) activity
.getSystemService(Activity.INPUT_METHOD_SERVICE);
// inputmanger.hideSoftInputFromWindow(view.getWindowToken(),0);

return inputmanger.isActive() && activity.getWindow().getCurrentFocus() != null;
}
return false;
}

/**
* 监听软键盘的高度
* @param activity
* @param listener
*/
public static void observeSoftKeyboard(Activity activity, final OnSoftKeyboardChangeListener listener) {
final View decorView = activity.getWindow().getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
int previousKeyboardHeight = -1;
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
int displayHeight = rect.bottom;
int height = decorView.getHeight();
int keyboardHeight = height - displayHeight;
if (previousKeyboardHeight != keyboardHeight) {
boolean hide = (double) displayHeight / height > 0.8;
listener.onSoftKeyBoardChange(keyboardHeight, !hide);
}

previousKeyboardHeight = height;

}
});
}

public interface OnSoftKeyboardChangeListener {
void onSoftKeyBoardChange(int softKeybardHeight, boolean visible);
}
}