Android软键盘显示隐藏事件监听

由于Android系统并没有提供软键盘弹入和弹出的系统方法,所以我们有时候需要用到监听软键盘的时候会非常的麻烦,根据监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变超过一定的数值来监听软键盘的弹入弹出事件,使用起来也非常简单。

自定义监听工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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();
Log.i("yezhou", ""+visibleHeight);
if (rootViewVisibleHeight == 0) {
rootViewVisibleHeight = visibleHeight;
return;
}

//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
if (rootViewVisibleHeight == visibleHeight) {
return;
}

//根视图显示高度变小超过300,可以看作软键盘显示了,该数值可根据需要自行调整
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}

//根视图显示高度变大超过300,可以看作软键盘隐藏了,该数值可根据需要自行调整
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);
}
}

AndroidManifest.xml配置

我们在需要使用监听的Activity清单文件中设置configChanges

1
2
3
4
<activity android:name=".MainActivity"
android:launchMode="singleTop"
android:configChanges="keyboardHidden"
android:windowSoftInputMode="adjustPan|stateHidden"/>

使用方法

最后,使用方法也非常的简单,只需要在Activity类中调用方法即可,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//监听软件盘是否弹起
private void onKeyBoardListener() {
SoftKeyBoardListener.setListener(MainActivity.this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
Log.e("yezhou", "键盘显示 高度" + height);
}

@Override
public void keyBoardHide(int height) {
Log.e("yezhou", "键盘隐藏 高度" + height);
}
});
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :