转载请注明出处
http://blog.youkuaiyun.com/u014513456/article/details/54343765
Author:ruanjianjiagou@163.com
背景
在京东金融APP的登陆页面键盘弹出后整个布局上移,键盘弹起后,用户仍能看到页面全部内容,这样的用户体验要好于键盘弹起后紧贴输入框下侧,或者键盘弹起后直接连输入的区域都遮挡。
技术点
遗憾的是系统并不提供键盘弹起的事件监听
1.键盘事件的监听
2.布局的移动
以下为解决方案
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="open.ppdai.com.keyboardlogin">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
以下是关键语句
<activity
android:name=".MainActivity" android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
Activity
package open.ppdai.com.keyboardlogin;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
/**
* Author ruanjianjiagou@163.com
* 捕捉界面键盘弹起动作
* 例如 京东金融 登陆界面 在输入框获取焦点后键盘弹出把整个布局上移
* 键盘回落后布局也相应回落
*
*/
public class MainActivity extends AppCompatActivity {
private LinearLayout root_view;
private int screenHeight = 0;
private int keyHeight = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
initOtherData();
}
private void initView() {
root_view = (LinearLayout) findViewById(R.id.root_view);
}
private void initListener() {
root_view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {
Toast.makeText(MainActivity.this,"键盘弹起",Toast.LENGTH_SHORT).show();
} else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {
Toast.makeText(MainActivity.this,"键盘落下",Toast.LENGTH_SHORT).show();
}
}
});
}
private void initOtherData() {
screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
keyHeight = screenHeight / 3;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context="open.ppdai.com.keyboardlogin.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="64dp"
android:background="@mipmap/ic_launcher" />
<EditText
android:id="@+id/user"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="username"
android:textColorHint="#cccccc" />
<EditText
android:id="@+id/pass"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="password"
android:textColorHint="#cccccc" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="register!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.502" />
<Button
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:text="login" />
</LinearLayout>
有了监听事件了,那剩下的就是布局移动了,请看下篇
Android 键盘弹起和回落事件监听(二)之移动布局
http://blog.youkuaiyun.com/u014513456/article/details/54378781
源码戳我下载
http://download.youkuaiyun.com/download/u014513456/9735954