在开发过程中,键盘将阻止输入框。您可以通过监视视图的更改来回调键盘是否弹出。
由于它正在监视由键盘引起的视图变化,因此需要设置以下条件才能进行监视
要设置getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
或在AndroidManifest.xml的活动标签中设置AdjustPan

有关详细信息,请参阅对Android软键盘的全面分析,以使您不再担心被覆盖的控件。
首先定义一个可检测键盘变化的类

/**
* 检测软键盘是否隐藏
* 要设置getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
* https://blog.csdn.net/l540675759/article/details/74528641
*/
public class SoftKeyBoardListener {
private View rootView;//activity的根视图
int rootViewVisibleHeight;//纪录根视图的显示高度
private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
private final ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener;
private SoftKeyBoardListener(Activity activity) {
//获取activity的根视图
rootView = activity.getWindow().getDecorView();
//获取当前根视图在屏幕上显示的大小
//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
//根视图显示高度变小超过200,可以看作软键盘显示了
//根视图显示高度变大超过200,可以看作软键盘隐藏了
mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//获取当前根视图在屏幕上显示的大小
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int visibleHeight = r.height();
if (rootViewVisibleHeight == 0) {
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
if (rootViewVisibleHeight == visibleHeight) {
return;
}
//根视图显示高度变小超过200,可以看作软键盘显示了
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度变大超过200,可以看作软键盘隐藏了
if (visibleHeight - rootViewVisibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
}
rootViewVisibleHeight = visibleHeight;
}
}
};
//视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
}
/**
* 移除全局,否则会内存泄漏
*/
public void removeGlobalLayoutListener() {
//视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
if (rootView != null && mOnGlobalLayoutListener != null)
rootView.getViewTreeObserver().removeOnGlobalLayoutListener(mOnGlobalLayoutListener);
}
private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
}
public interface OnSoftKeyBoardChangeListener {
void keyBoardShow(int height);
void keyBoardHide(int height);
}
public static SoftKeyBoardListener setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
return softKeyBoardListener;
}
}
这是用于键盘弹出和消失检测的工具。当检测到键盘弹出窗口时,将向上移动的添加到登录界面的布局中。
登录界面的布局xml

一个非常简单的登录界面,在获取布局对象后,执行属性设置
3.在活动中被调用
private SoftKeyBoardListener softKeyBoardListener;
@Override
protected void onCreate() {
super.onCreate();
//绑定
softKeyBoardListener = SoftKeyBoardListener.setListener(this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
ObjectAnimator valueAnimator = ObjectAnimator.ofFloat(mLoginRoot, "translationY", -100);
valueAnimator.setDuration(300);
valueAnimator.start();
}
@Override
public void keyBoardHide(int height) {
ObjectAnimator valueAnimator = ObjectAnimator.ofFloat(mLoginRoot, "translationY", 0);
valueAnimator.setDuration(300);
valueAnimator.start();
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
//解绑
softKeyBoardListener.removeGlobalLayoutListener();
}
这完成了这次的任务。弹出键盘时,要求登录界面布局向上移动一定距离,而隐藏键盘时,它将返回到原始位置。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-355319-1.html
统一是早晚的事
中国军事要坚定的强势发展