- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathInputMethodUtils.java
More file actions
Latest commit
116 lines (102 loc) · 3.82 KB
/
Copy pathInputMethodUtils.java
File metadata and controls
116 lines (102 loc) · 3.82 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
packagecom.realmo.utils;
importandroid.app.Activity;
importandroid.content.Context;
importandroid.graphics.Rect;
importandroid.util.Log;
importandroid.view.View;
importandroid.view.ViewTreeObserver;
importandroid.view.inputmethod.InputMethodManager;
publicclassInputMethodUtils {
/**
* 显示软键盘
*/
publicstaticvoidshowInputMethod(Viewview) {
InputMethodManagerimm = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
/**
* 显示软键盘
*/
publicstaticvoidshowInputMethod(Contextcontext) {
InputMethodManagerimm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 多少时间后显示软键盘
*/
publicstaticvoidshowInputMethod(finalViewview, longdelayMillis) {
if (view == null) return;
// 显示输入法
view.postDelayed(newRunnable() {
@Override
publicvoidrun() {
InputMethodUtils.showInputMethod(view);
}
}, delayMillis);
}
/**
* 隐藏软键盘
*/
publicstaticvoidclose(Activityactivity) {
if (activity == null) {
return;
}
Viewview = activity.getWindow().getDecorView().getRootView();
try {
InputMethodManagerimm = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
} catch (Exceptione) {
e.printStackTrace();
}
}
/**
* 隐藏软键盘
*/
publicstaticvoidclose(Viewview) {
try {
InputMethodManagerimm = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
} catch (Exceptione) {
e.printStackTrace();
}
}
publicstaticvoidobserverKeyboardVisibleChange(finalViewdecorView, finalOnKeyboardStateChangeListenerlistener) {
if (decorView == null || listener == null) return;
decorView.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {
intpreKeyboardHeight = -1;
Rectrect = newRect();
booleanpreVisible = false;
@Override
publicvoidonGlobalLayout() {
rect.setEmpty();
decorView.getWindowVisibleDisplayFrame(rect);
intdisplayHeight = rect.height();
intwindowHeight = decorView.getHeight();
intkeyboardHeight = windowHeight - displayHeight;
if (preKeyboardHeight != keyboardHeight) {
//判定可见区域与原来的window区域占比是否小于0.75,小于意味着键盘弹出来了。
booleanisVisible = (displayHeight * 1.0f / windowHeight * 1.0f) < 0.75f;
if (isVisible != preVisible) {
listener.onKeyboardChange(keyboardHeight, isVisible);
preVisible = isVisible;
}
}
preKeyboardHeight = keyboardHeight;
}
});
}
//=============================================================interface
publicinterfaceOnKeyboardStateChangeListener {
voidonKeyboardChange(intkeyboardHeight, booleanisVisible);
}
}