实现软键盘弹出,登陆按钮以上全部上推;下面直接贴代码
布局:里面的dimen自己设定吧,很简单
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollview_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_login">
<ImageView
android:id="@+id/iv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingTop="@dimen/logo_padding_top"
android:src="@drawable/bg_login_logo" />
<RelativeLayout
android:id="@+id/ll_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_logo"
android:layout_marginRight="@dimen/ll_margin_left"
android:layout_marginTop="@dimen/ll_margin_top">
<EditText
android:id="@+id/et_mobile"
style="@style/CommonEditTextStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/et_margin_left"
android:background="@null"
android:drawableLeft="@drawable/selector_phone"
android:drawablePadding="@dimen/drawable_padding"
android:hint="@string/forget_pay_psd_phone_desc"
android:inputType="number"
android:lines="1" />
<View
android:id="@+id/view_112"
android:layout_width="match_parent"
android:layout_height="@dimen/line_h"
android:layout_below="@+id/et_mobile"
android:layout_marginLeft="@dimen/line_left"
android:layout_marginTop="@dimen/line_top"
android:background="@color/c4" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/ll_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_1"
android:layout_marginRight="@dimen/ll_margin_left">
<EditText
android:id="@+id/et_pwd_login"
style="@style/CommonEditTextStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/et_margin_left"
android:layout_marginTop="@dimen/line_top"
android:background="@null"
android:drawableLeft="@drawable/selector_password"
android:drawablePadding="@dimen/drawable_padding"
android:hint="@string/prompt_login_pwd"
android:inputType="textPassword"
android:lines="1" />
<View
android:id="@+id/view_113"
android:layout_width="match_parent"
android:layout_height="@dimen/line_h"
android:layout_below="@+id/et_pwd_login"
android:layout_marginLeft="@dimen/line_left"
android:layout_marginTop="@dimen/line_top"
android:background="@color/c4" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_verification_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ll_2"
android:layout_marginRight="@dimen/ll_margin_left">
<EditText
android:id="@+id/et_code_login"
style="@style/CommonEditTextStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/et_margin_left"
android:layout_marginTop="@dimen/line_top"
android:layout_toLeftOf="@+id/iv_login_checkcode"
android:background="@null"
android:drawableLeft="@drawable/selector_checkcode"
android:drawablePadding="@dimen/drawable_padding"
android:hint="@string/input_image_checkcode"
android:inputType="number"
android:lines="1" />
<ImageView
android:id="@+id/iv_login_checkcode"
android:layout_width="@dimen/iv_checkcode_w"
android:layout_height="@dimen/iv_checkcode_h"
android:layout_marginBottom="@dimen/checkcode_margin"
android:layout_marginTop="@dimen/checkcode_margin"
android:layout_toLeftOf="@+id/iv_refresh" />
<ImageView
android:id="@+id/iv_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingBottom="@dimen/refresh_bottom"
android:paddingLeft="@dimen/refresh_left"
android:paddingTop="@dimen/line_top"
android:src="@drawable/ic_refresh" />
<View
android:id="@+id/view_114"
android:layout_width="match_parent"
android:layout_height="@dimen/line_h"
android:layout_below="@+id/iv_refresh"
android:layout_marginLeft="@dimen/line_left"
android:background="@color/c4" />
</RelativeLayout>
<TextView
android:id="@+id/tv_findback_pwd"
style="@style/d3c1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/rl_verification_code"
android:layout_marginRight="@dimen/btn_margin"
android:layout_marginTop="@dimen/close_padding_top"
android:padding="@dimen/tv_padding"
android:text="忘记密码" />
<Button
android:id="@+id/btn_login"
style="@style/d2cwhite"
android:layout_width="match_parent"
android:layout_height="@dimen/common_btn_height"
android:layout_below="@+id/tv_findback_pwd"
android:layout_marginBottom="@dimen/btn_margin"
android:layout_marginLeft="@dimen/btn_margin"
android:layout_marginRight="@dimen/btn_margin"
android:layout_marginTop="@dimen/btn_margin_top"
android:background="@drawable/selector_circle_red_btn"
android:text="登 录" />
</RelativeLayout>
</ScrollView>
<ImageView
android:id="@+id/iv_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/close_padding_left"
android:paddingRight="@dimen/close_padding_right"
android:paddingTop="@dimen/close_padding_top"
android:src="@drawable/ic_login_close" />
<TextView
android:id="@+id/tv_go_register"
style="@style/d2c2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/iv_close"
android:layout_alignParentRight="true"
android:paddingLeft="@dimen/close_padding_left"
android:paddingRight="@dimen/close_padding_right"
android:paddingTop="@dimen/close_padding_top"
android:text="@string/go_register" />
</RelativeLayout>
核心方法:
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
/**
*.
* 实现弹出软键盘时 整个布局向上平移,解决遮挡问题
* 在onCreate中添加监听,在onDestroy中remove监听
*/
public class CustomGlobalLayoutListener implements OnGlobalLayoutListener {
private Context mContext;
private View mRootView;
private View mScrollToView;
/**
* @param context context
* @param rootView 可以滚动的布局
* @param scrollToView 界面上被遮挡的位于最底部的布局(控件)
*/
public CustomGlobalLayoutListener(Context context, View rootView, View scrollToView) {
this.mContext = context;
this.mRootView = rootView;
this.mScrollToView = scrollToView;
}
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
mRootView.getWindowVisibleDisplayFrame(rect);
int rootInvisibleHeight = mRootView.getRootView().getHeight() - rect.bottom;
if (rootInvisibleHeight > 100) {
int[] location = new int[2];
mScrollToView.getLocationInWindow(location);
int scrollHeight = (location[1] + mScrollToView.getHeight()) - rect.bottom;
mRootView.scrollTo(0, scrollHeight + Utils.dip2px(mContext, 30));
} else {
mRootView.scrollTo(0, 0);
}
}
}
应用在Activity中:
private CustomGlobalLayoutListener customGlobalLayoutListener;
customGlobalLayoutListener = new CustomGlobalLayoutListener(this, scrollView, mBtnNextStep);
scrollView.getViewTreeObserver().addOnGlobalLayoutListener(customGlobalLayoutListener);
@Override
protected void onDestroy() {//防止内存溢出
super.onDestroy();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(customGlobalLayoutListener);
} else {
scrollView.getViewTreeObserver().removeGlobalOnLayoutListener(customGlobalLayoutListener);
}
}
配置文件中:
android:windowSoftInputMode="stateHidden|adjustResize"
缺点:
布局会压缩