private RelativeLayout.LayoutParams layoutParams; // 输入布局的布局参数
private int KeyboardHeight; // 键盘的高度
private int currentState; // 软键盘当前的状态
private int rootTabHeight;// 底部rootTab的高度
// 当界面绘制完场后,计算布局的相关参数
inputReplyRL.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
inputReplyRL.getViewTreeObserver().removeOnGlobalLayoutListener(this);
layoutParams = (RelativeLayout.LayoutParams) inputReplyRL.getLayoutParams();
rootTabHeight = (int) context.getResources().getDimension(R.dimen.design_bottom_navigation_height); // 底部rootTab的高度
KeyboardHeight = PreferenceUtils.getRecordKeyboardHeight() - rootTabHeight;
replyMessageBET.requestFocus(); // 输入框获取焦点
KeyboardUtil.showInputMethod(replyMessageBET); // 弹出软键盘
getActivity().getWindow().getDecorView()
.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
//获取View可见区域的bottom
Rect rect = new Rect();
getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
if (bottom != 0 && oldBottom != 0 && bottom - rect.bottom <= 0) {
CustomToast.makeText(context, "隐藏", Toast.LENGTH_SHORT).show();
setKetBoardState(KET_BOARD_STATE_HINT);
} else {
CustomToast.makeText(context, "弹出", Toast.LENGTH_SHORT).show();
setKetBoardState(KET_BOARD_STATE_SHOW);
}
});
}
});
// 监听布局绘制变化的回调
getActivity().getWindow().getDecorView()
.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
//获取View可见区域的bottom
Rect rect = new Rect();
getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
if (bottom != 0 && oldBottom != 0 && bottom - rect.bottom <= 0) {
CustomToast.makeText(context, "隐藏", Toast.LENGTH_SHORT).show();
setKetBoardState(KET_BORARD_STATE_HINT);
} else {
CustomToast.makeText(context, "弹出", Toast.LENGTH_SHORT).show();
setKetBoardState(KET_BORARD_STATE_SHOW);
}
});
/**
* 设置软键盘与输入框的状态
*
* @param state 状态
*/
private void setKetBoardState(int state) {
if (currentState == state) {
return;
}
switch (state) {
case KET_BORARD_STATE_HINT:
showOrHintKeyBoard(false);
break;
case KET_BORARD_STATE_SHOW:
showOrHintKeyBoard(true);
break;
}
currentState = state;
}
/**
* 设置软键盘隐藏或者显示时,输入框的弹起高度
*
* @param isSHow 软键盘是否显示
*/
private void showOrHintKeyBoard(boolean isSHow) {
layoutParams.setMargins(0, 0, 0, isSHow ? KeyboardHeight : 0);
inputReplyRL.setLayoutParams(layoutParams);
}
输入框与软键盘一起控制的方法
最新推荐文章于 2022-09-16 16:01:56 发布