登录界面键盘弹出时布局向上移动

在开发过程中会出现键盘遮挡输入框的情况,可以通过监测视图的变化进行回调键盘是否弹起
既然是监测键盘引起视图的变化那就需要设置以下条件才可以进行监听

要设置getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

或者在AndroidManifest.xmlactivity标签设置adjustPan

<activity
android:name=".Activity"
android:windowSoftInputMode="stateAlwaysHidden|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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/login_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:gravity="center_horizontal"
android:orientation="vertical">

<ImageView
android:id="@+id/loading_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="70dp"
android:src="@drawable/logo" />

<LinearLayout
android:id="@+id/ll_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:background="@drawable/login_edit"
android:orientation="horizontal">

<EditText
android:id="@+id/et_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:background="@null"
android:hint="请输入账号"
android:padding="12dp"
android:textColor="#000"
android:textColorHint="#b5b5b5"
android:textSize="14sp" />

<CheckBox
android:id="@+id/cb_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="记住账号"
android:textColor="#32aaff" />
</LinearLayout>

<LinearLayout
android:id="@+id/ll_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/login_edit"
android:orientation="horizontal">

<EditText
android:id="@+id/et_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:background="@null"
android:hint="请输入密码"
android:inputType="textPassword"
android:padding="12dp"
android:textColor="#000"
android:textColorHint="#b5b5b5"
android:textSize="14sp" />

<CheckBox
android:id="@+id/cb_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="记住密码"
android:textColor="#32aaff" />
</LinearLayout>

<Button
android:id="@+id/login"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/selector_button"
android:padding="12dp"
android:text="登陆"
android:textColor="#ffffff"
android:textSize="14sp" />
</LinearLayout>

很简单的一个登录界面,获取到跟布局的对象之后进行一个属性动画设置

3.在activity中调用

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();
}

这样就完成了本次的任务, 当键盘弹出的时候就叫登录界面布局向上移动一些距离,当键盘隐藏的时候就恢复到原来的位置.

0%