Skip to content

Commit 6c0e953

Browse files
committed
add touch blank area to hide keyboard
1 parent 324f354 commit 6c0e953

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
- [键盘相关](https://github.com/Blankj/AndroidUtilCode/blob/master/about_keyboard.md)
4848
- 避免输入法面板遮挡
4949
- 动态隐藏软键盘
50+
- 点击屏幕空白区域隐藏软键盘
5051
- 动态显示软键盘
5152
- 切换键盘显示与否状态
5253
- [正则相关](https://github.com/Blankj/AndroidUtilCode/blob/master/about_regular.md)
@@ -57,4 +58,5 @@
5758
- [未归类](https://github.com/Blankj/AndroidUtilCode/blob/master/unclassified.md)
5859
- 获取服务是否开启
5960

61+
大部分代码已验证过可行,如有错误,请及时告之。
6062
期待你的Star和完善...

about_keyboard.md

+55
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,61 @@ public static void hideSoftInput(Context context, EditText edit) {
3030
}
3131
```
3232

33+
### 点击屏幕空白区域隐藏软键盘
34+
``` java
35+
// 方法1:在onTouch中处理,未获焦点则隐藏
36+
/**
37+
* 在onTouch中处理,未获焦点则隐藏
38+
*/
39+
@Override
40+
public boolean onTouchEvent(MotionEvent event) {
41+
if (null != this.getCurrentFocus()) {
42+
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
43+
return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
44+
}
45+
return super.onTouchEvent(event);
46+
}
47+
48+
// 方法2:根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘,需重写dispatchTouchEvent
49+
@Override
50+
public boolean dispatchTouchEvent(MotionEvent ev) {
51+
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
52+
View v = getCurrentFocus();
53+
if (isShouldHideKeyboard(v, ev)) {
54+
hideKeyboard(v.getWindowToken());
55+
}
56+
}
57+
return super.dispatchTouchEvent(ev);
58+
}
59+
60+
/**
61+
* 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘
62+
*/
63+
private boolean isShouldHideKeyboard(View v, MotionEvent event) {
64+
if (v != null && (v instanceof EditText)) {
65+
int[] l = {0, 0};
66+
v.getLocationInWindow(l);
67+
int left = l[0],
68+
top = l[1],
69+
bottom = top + v.getHeight(),
70+
right = left + v.getWidth();
71+
return !(event.getX() > left && event.getX() < right
72+
&& event.getY() > top && event.getY() < bottom);
73+
}
74+
return false;
75+
}
76+
77+
/**
78+
* 获取InputMethodManager,隐藏软键盘
79+
*/
80+
private void hideKeyboard(IBinder token) {
81+
if (token != null) {
82+
InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
83+
im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
84+
}
85+
}
86+
```
87+
3388
### 动态显示软键盘
3489
``` java
3590
/**

about_phone.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,30 @@ public static String getDeviceIMEI(Context context) {
3232
// 需添加权限<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
3333
/**
3434
* 获取手机状态信息
35+
*
36+
* 返回如下
37+
* DeviceId(IMEI) = 99000311726612
38+
* DeviceSoftwareVersion = 00
39+
* Line1Number =
40+
* NetworkCountryIso = cn
41+
* NetworkOperator = 46003
42+
* NetworkOperatorName = 中国电信
43+
* NetworkType = 6
44+
* honeType = 2
45+
* SimCountryIso = cn
46+
* SimOperator = 46003
47+
* SimOperatorName = 中国电信
48+
* SimSerialNumber = 89860315045710604022
49+
* SimState = 5
50+
* SubscriberId(IMSI) = 460030419724900
51+
* VoiceMailNumber = *86
3552
*/
3653
public static String getPhoneStatus(Context context) {
3754
TelephonyManager tm = (TelephonyManager) context
38-
.getSystemService(Context.TELEPHONY_SERVICE);//
55+
.getSystemService(Context.TELEPHONY_SERVICE);
3956
String str = "";
4057
str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";
41-
str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion()
42-
+ "\n";
58+
str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion() + "\n";
4359
str += "Line1Number = " + tm.getLine1Number() + "\n";
4460
str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";
4561
str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";

0 commit comments

Comments
 (0)