login页面,输入框获得焦点后弹出软键盘,如果软键盘遮住登录按钮,体验就不好了,所以要把登录按钮以上的布局顶上去。
页面布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_ffffff"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="@string/login_phone"
android:textSize="15sp"/>
<EditText
android:id="@+id/login_et_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:textSize="15sp"
android:hint="@string/login_phone_hint"
android:maxLength="11"
android:inputType="number"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:text="@string/login_msg_code"/>
<EditText
android:id="@+id/login_et_msg_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"/>
</LinearLayout>
<RadioButton
android:id="@+id/login_btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_marginLeft="26dp"
android:layout_marginRight="26dp"
android:paddingTop="12dp"
android:checked="true"
android:paddingBottom="12dp"
android:button="@null"
android:text="@string/login"
android:textSize="16sp"
android:textColor="@color/color_ffffff"
android:background="@drawable/rb_login_bg"/>
</LinearLayout>
login_activity里定义全局变量
/**登录按钮的location坐标的y值,用来计算软键盘弹出后rootview向上滑动的高度*/
private int btnY = 0;
在login_activity的onCreate()方法里调用下面方法:
//输入法软键盘弹出时,把整个布局顶上去
controlKeyboardLayout(main_layout, login_btn_login);
/**
* @param root
* 最外层布局,需要调整的布局
* @param scrollToView
* 被键盘遮挡的scrollToView,滚动root,使scrollToView在root可视区域的底部
*/
private void controlKeyboardLayout(final View root, final View scrollToView) {
// 注册一个回调函数,当在一个视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变时调用这个回调函数。
root.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
// 获取root在窗体的可视区域
root.getWindowVisibleDisplayFrame(rect);
// 当前视图最外层的高度减去现在所看到的视图的最底部的y坐标
int rootInvisibleHeight = root.getRootView()
.getHeight() - rect.bottom;
Log.i("tag", "最外层的高度" + root.getRootView().getHeight());
Log.i("tag","bottom的高度" + rect.bottom);
// 若rootInvisibleHeight高度大于100,则说明当前视图上移了,说明软键盘弹出了
if (rootInvisibleHeight > 100) {
//软键盘弹出来的时候
int[] location = new int[2];
// 获取scrollToView在窗体的坐标
scrollToView.getLocationInWindow(location);
//btnY的初始值为0,一旦赋过一次值就不再变化
if (btnY == 0){
btnY = location[1];
}
// 计算root滚动高度,使scrollToView在可见区域的底部
int srollHeight = (btnY + scrollToView
.getHeight()) - rect.bottom;
root.scrollTo(0, srollHeight);
} else {
// 软键盘没有弹出来的时候
root.scrollTo(0, 0);
}
}
});
}
btnY的作用很关键。
如果没有设定btnY,则随着输入框被点击,布局反复计算srollHeight,会出现点击输入框,布局弹上去,再点击一下输入框,布局又落下来的状态。