原理:先用ScrollView包在最外面,然后通过监视它的高度 getViewTreeObserver().addOnGlobalLayoutListener,最后调用scrollTo
主配置文件代码:
<activity
android:name="com.sharera.capitalcircle.activity.LoginActivity"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
前台页面:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/login_rootlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dd4443"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/login_log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/login_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/login_log"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="100dp"
android:orientation="vertical" >
<EditText
android:id="@+id/login_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:hint="邮箱/手机号"
android:maxLength="20" />
<EditText
android:id="@+id/login_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:hint="请输入密码"
android:inputType="textPassword"
android:maxLength="20" />
<Button
android:id="@+id/login_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/login_button"
android:text="登录"
android:textColor="#ffffff"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<CheckBox
android:id="@+id/login_remember_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<CheckBox
android:id="@+id/login_autolgoin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="自动登录" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
后台关键方法:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
//只绑定一次
if (hasFocus && firstLoad) {
firstLoad = false;
layout = (ScrollView) findViewById(R.id.login_rootlayout);
layout.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//绑定后会触发一次,所以第一次不滚动。
if (firstChange) {
firstChange = false;
layoutHeight=layout.getMeasuredHeight();
} else {
int h=layout.getMeasuredHeight();
if(h<layoutHeight)
{
layout.scrollTo(0, layoutHeight);
}
}
}
});
}
}
这个效果是一开始没有键盘显示,点击输入框才会弹出软键盘,页面整体上移,如果没有adjustResize,layout.scrollTo不会触发,如果不加stateAlwaysHidden,屏幕加载完会直接弹键盘。