这几天项目有个需求,就是当输入框输入的时候,需要把界面顶上去腾出键盘位置,不然键盘挡住界面,然后查找了各种方法:网上大多介绍android:windowSoftInputMode嵌套ScrollView综合使用来实现,然后搞了一下,却没能实现功能(参考:http://blog.csdn.net/lingdianalex/article/details/52411317)
后来找了其他方法:
在网上找了一个方法(原创连接忘记是那个了,如原创连接主人看到可联系小木写上您的连接),该方法是根据activity的可视界面的大小变化监听,从而判断键盘是否是弹出()
package tsjk.com.tsjkhis.utils;
import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
/**
* Created by Administrator on 2017/12/22.
*/
public class SoftKeyBoardListener {
private View rootView;//activity的根视图
int rootViewVisibleHeight;//纪录根视图的显示高度
public OnSoftKeyBoardChangeListener getOnSoftKeyBoardChangeListener() {
return onSoftKeyBoardChangeListener;
}
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();
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);
}
}
本方法的关键点activity的可视范围的监听回调:
//获取activity的根视图
rootView = activity.getWindow().getDecorView();
//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {然后获取当前布局的高度与之前的高度对比判断是否有变化(200的阈值),根据变化值判断键盘隐藏或者显示 然后创建接口回调参数:
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);
}
到这里,基本可以实现视图键盘的监听了,但是小木的项目是有横竖屏兼容,当横竖屏切换的时候,该方法也会调用,而且变化超过了200,会触发该监听。最后的思路是:回调触发是获取当前横竖屏状态,判断是否是横竖屏切换,如果是,则都隐藏键盘,如果不是,则实现键盘的显示隐藏:
//判断横竖屏的参数 -1初始化 0表示横屏 1表示视屏
private int verticalOrhorizontal = -1;
//当前的状态
private int currverticalOrhorizontal = -1;
//输入法管理器
InputMethodManager imm;
SoftKeyBoardListener.setListener(MainActivity.this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
getOrientation();
if (verticalOrhorizontal == currverticalOrhorizontal) {
LogUtil.e("keyBoard", "keyBoardShow" + height);
//显示键盘占位符
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
space.setLayoutParams(params);
space.setVisibility(View.VISIBLE);
} else {
//横竖屏的时候收起键盘
if (space != null) {
space.setVisibility(View.GONE);
currverticalOrhorizontal = verticalOrhorizontal;
if (imm != null) {
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
}
}
}
}
@Override
public void keyBoardHide(int height) {
getOrientation();
if (verticalOrhorizontal == currverticalOrhorizontal) {
//隐藏键盘占位符
if (space.getVisibility() == View.VISIBLE) {
space.setVisibility(View.GONE);
}
} else {
//横竖屏的时候收起键盘
if (space != null) {
space.setVisibility(View.GONE);
currverticalOrhorizontal = verticalOrhorizontal;
if (imm != null) {
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
}
}
}
}
});
}
private void getOrientation() {
// 判断Android当前的屏幕是横屏还是竖屏。横竖屏判断
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//竖屏
verticalOrhorizontal = 1;
} else {
//横屏
verticalOrhorizontal = 0;
}
}
从而满足了需求(小木的项目是webview加载网页,同事网页端做了适配处理,所以小木只需要把webview顶上去,不让键盘遮住就好了)布局:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/lin_tab"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="@android:color/black"
android:orientation="horizontal"
android:visibility="gone">LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/lin_tab"
android:orientation="vertical">
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/lin_tab"
android:layout_weight="1"
android:visibility="visible">FrameLayout>
<LinearLayout
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:orientation="horizontal">LinearLayout>
LinearLayout>
<Button
android:id="@+id/btn_one"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:background="@drawable/shuxin" />
RelativeLayout>