最近有个这样的需求,说点击每一个edittext软件盘弹起的时候都不能挡住所有的输入布局,需要往上顶,但是头部的标题栏又不能动,如下图所示找了好多资料都会把头部给顶上去,最后想到了一个办法,将标题栏下面的内容的最外层套上ScrollView,然后监听软键盘弹起的高度,动态设置phone一栏距离底部的高度。实现了这个需求,代码如下
1、布局如下:
<?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"
android:background="@color/colorBackground"
android:orientation="vertical"
android:fitsSystemWindows="true">
<include layout="@layout/layout_title_bar"
android:id="@+id/tv_titleBar"
/>
<ScrollView
android:id="@+id/sc_scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scrollbars="none"
android:layout_below="@+id/tv_titleBar">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="150dp"
android:orientation="vertical"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<com.auratech.base.common.widget.CircleImageView
android:id="@+id/iv_headView"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/add_contact_head_icon" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/line_bg_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/c11"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:minHeight="60dp"
android:orientation="horizontal"
android:paddingLeft="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name :"
android:textColor="@color/color_333333"
android:textSize="@dimen/text_size32" />
<EditText
android:id="@+id/et_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:gravity="center_vertical"
android:inputType="text"
android:maxLength="20"
android:singleLine="true"
android:textColor="@color/colorLoadingText"
android:textCursorDrawable="@drawable/edit_cursor_bg"
android:textSize="16dp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:background="@color/line_bg_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/c11"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:minHeight="60dp"
android:orientation="horizontal"
android:paddingLeft="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Note :"
android:textColor="@color/color_333333"
android:textSize="@dimen/text_size32" />
<EditText
android:id="@+id/et_note"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="@color/colorLoadingText"
android:textCursorDrawable="@drawable/edit_cursor_bg"
android:textSize="16dp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:background="@color/line_bg_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:minHeight="60dp"
android:orientation="horizontal"
android:paddingLeft="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone :"
android:textColor="@color/colorLoadingText"
android:textSize="16dp" />
<EditText
android:id="@+id/et_phone"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:gravity="center_vertical"
android:inputType="phone"
android:maxLength="20"
android:singleLine="true"
android:textColor="@color/colorLoadingText"
android:textCursorDrawable="@drawable/edit_cursor_bg"
android:textSize="16dp" />
</LinearLayout>
<TextView
android:id="@+id/tv_phone_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:background="@color/line_bg_color" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
2、自定义软键盘的监听:
package com.auratech.contacts.ui.activity;
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
/**
* <pre>
* author : Ben
* e-mail : ben-lau@auratech.hk
* time : 2019/03/22
* desc :
* version: 1.0
* </pre>
*/
public class SoftKeyBoardListener {
private View rootView;//activity的根视图
int rootViewVisibleHeight;//纪录根视图的显示高度
private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
public SoftKeyBoardListener(Activity activity) {
//获取activity的根视图
rootView = activity.getWindow().getDecorView();
//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//获取当前根视图在屏幕上显示的大小
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int visibleHeight = r.height();
System.out.println(""+visibleHeight);
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;
return;
}
}
});
}
private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
}
public interface OnSoftKeyBoardChangeListener {
void keyBoardShow(int height);
void keyBoardHide(int height);
}
public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
}
}
3、最后在activity中的主要代码如下:
监听软键盘起来的高度,动态设置phone一栏距离底部的高度,最后将ScrollView自动回滚到最后
SoftKeyBoardListener.setListener(this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
Log.e(TAG, "键盘显示 高度" + height);
tv_phone_line.setPadding(0, 0, 0, height);
sc_scrollView.fullScroll(ScrollView.FOCUS_DOWN);//滚动到底部
}
@Override
public void keyBoardHide(int height) {
Log.e(TAG, "键盘隐藏 高度" + height);
tv_phone_line.setPadding(0, 0, 0, height);
}
});
以上方法仅作为个人笔记,并且只适合这种少输入控件的布局页面,要不然会将你要输入的一栏顶到最上面去不可见了