-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathInputMethodUtils.java
More file actions
116 lines (102 loc) · 3.82 KB
/
InputMethodUtils.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
package com.realmo.utils;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
public class InputMethodUtils {
/**
* 显示软键盘
*/
public static void showInputMethod(View view) {
InputMethodManager imm = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
/**
* 显示软键盘
*/
public static void showInputMethod(Context context) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 多少时间后显示软键盘
*/
public static void showInputMethod(final View view, long delayMillis) {
if (view == null) return;
// 显示输入法
view.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodUtils.showInputMethod(view);
}
}, delayMillis);
}
/**
* 隐藏软键盘
*/
public static void close(Activity activity) {
if (activity == null) {
return;
}
View view = activity.getWindow().getDecorView().getRootView();
try {
InputMethodManager imm = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 隐藏软键盘
*/
public static void close(View view) {
try {
InputMethodManager imm = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void observerKeyboardVisibleChange(final View decorView, final OnKeyboardStateChangeListener listener) {
if (decorView == null || listener == null) return;
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
int preKeyboardHeight = -1;
Rect rect = new Rect();
boolean preVisible = false;
@Override
public void onGlobalLayout() {
rect.setEmpty();
decorView.getWindowVisibleDisplayFrame(rect);
int displayHeight = rect.height();
int windowHeight = decorView.getHeight();
int keyboardHeight = windowHeight - displayHeight;
if (preKeyboardHeight != keyboardHeight) {
//判定可见区域与原来的window区域占比是否小于0.75,小于意味着键盘弹出来了。
boolean isVisible = (displayHeight * 1.0f / windowHeight * 1.0f) < 0.75f;
if (isVisible != preVisible) {
listener.onKeyboardChange(keyboardHeight, isVisible);
preVisible = isVisible;
}
}
preKeyboardHeight = keyboardHeight;
}
});
}
//=============================================================interface
public interface OnKeyboardStateChangeListener {
void onKeyboardChange(int keyboardHeight, boolean isVisible);
}
}