Skip to content

Commit 9f03de6

Browse files
committed
see 06/01 log
1 parent 5e811f6 commit 9f03de6

File tree

9 files changed

+183
-34
lines changed

9 files changed

+183
-34
lines changed

README-CN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@ getCaptureIntent : 获取拍照的意图
317317

318318
* ### 键盘相关→[KeyboardUtils.java][keyboard.java][Demo][keyboard.demo]
319319
```
320-
hideSoftInput : 动态隐藏软键盘
321-
clickBlankArea2HideSoftInput: 点击屏幕空白区域隐藏软键盘
322320
showSoftInput : 动态显示软键盘
321+
hideSoftInput : 动态隐藏软键盘
323322
toggleSoftInput : 切换键盘显示与否状态
323+
clickBlankArea2HideSoftInput: 点击屏幕空白区域隐藏软键盘
324324
```
325325

326326
* ### 定位相关→[LocationUtils.java][location.java][Demo][location.demo]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@ getCaptureIntent
317317

318318
* ### About Keyboard→[KeyboardUtils.java][keyboard.java][Demo][keyboard.demo]
319319
```
320-
hideSoftInput
321-
clickBlankArea2HideSoftInput
322320
showSoftInput
321+
hideSoftInput
323322
toggleSoftInput
323+
clickBlankArea2HideSoftInput
324324
```
325325

326326
* ### About Location→[LocationUtils.java][location.java][Demo][location.demo]

app/src/main/java/com/blankj/androidutilcode/activity/KeyboardActivity.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.graphics.Rect;
55
import android.os.Bundle;
6+
import android.support.v7.app.AlertDialog;
67
import android.util.DisplayMetrics;
78
import android.view.MotionEvent;
89
import android.view.View;
@@ -12,6 +13,7 @@
1213

1314
import com.blankj.androidutilcode.R;
1415
import com.blankj.androidutilcode.base.BaseActivity;
16+
import com.blankj.androidutilcode.dialog.KeyboardDialog;
1517
import com.blankj.utilcode.util.KeyboardUtils;
1618
import com.blankj.utilcode.util.LogUtils;
1719

@@ -26,6 +28,8 @@
2628
public class KeyboardActivity extends BaseActivity {
2729

2830
TextView tvAboutKeyboard;
31+
EditText etInput;
32+
private AlertDialog dialog;
2933

3034
@Override
3135
public void initData(Bundle bundle) {
@@ -39,9 +43,11 @@ public int bindLayout() {
3943

4044
@Override
4145
public void initView(Bundle savedInstanceState, View view) {
46+
etInput = (EditText) findViewById(R.id.et_input);
4247
findViewById(R.id.btn_hide_soft_input).setOnClickListener(this);
4348
findViewById(R.id.btn_show_soft_input).setOnClickListener(this);
4449
findViewById(R.id.btn_toggle_soft_input).setOnClickListener(this);
50+
findViewById(R.id.btn_keyboard_in_fragment).setOnClickListener(this);
4551
tvAboutKeyboard = (TextView) findViewById(R.id.tv_about_keyboard);
4652
}
4753

@@ -57,11 +63,15 @@ public void onWidgetClick(View view) {
5763
KeyboardUtils.hideSoftInput(this);
5864
break;
5965
case R.id.btn_show_soft_input:
60-
KeyboardUtils.showSoftInput((EditText) findViewById(R.id.et));
66+
KeyboardUtils.showSoftInput(this);
6167
break;
6268
case R.id.btn_toggle_soft_input:
6369
KeyboardUtils.toggleSoftInput();
6470
break;
71+
case R.id.btn_keyboard_in_fragment:
72+
KeyboardUtils.hideSoftInput(this);
73+
new KeyboardDialog(this).show();
74+
break;
6575
}
6676
}
6777

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.blankj.androidutilcode.dialog;
2+
3+
import android.app.Activity;
4+
import android.support.v7.app.AlertDialog;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.widget.EditText;
8+
9+
import com.blankj.androidutilcode.R;
10+
import com.blankj.utilcode.util.KeyboardUtils;
11+
12+
import java.lang.ref.WeakReference;
13+
14+
/**
15+
* <pre>
16+
* author: Blankj
17+
* blog : http://blankj.com
18+
* time : 2017/06/01
19+
* desc :
20+
* </pre>
21+
*/
22+
public class KeyboardDialog implements View.OnClickListener {
23+
24+
private WeakReference<Activity> mActivityWeakReference;
25+
26+
public KeyboardDialog(Activity activity) {
27+
mActivityWeakReference = new WeakReference<Activity>(activity);
28+
}
29+
30+
private AlertDialog dialog;
31+
private EditText etInput;
32+
33+
public void show(){
34+
final Activity activity = mActivityWeakReference.get();
35+
if (activity != null) {
36+
final View dialogView = LayoutInflater.from(activity).inflate(R.layout.dialog_keyboard, null);
37+
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
38+
etInput = (EditText) dialogView.findViewById(R.id.et_input);
39+
dialog = builder.setView(dialogView).create();
40+
dialog.setCanceledOnTouchOutside(false);
41+
dialogView.findViewById(R.id.btn_hide_soft_input).setOnClickListener(this);
42+
dialogView.findViewById(R.id.btn_show_soft_input).setOnClickListener(this);
43+
dialogView.findViewById(R.id.btn_toggle_soft_input).setOnClickListener(this);
44+
dialogView.findViewById(R.id.btn_close_dialog).setOnClickListener(this);
45+
dialog.show();
46+
KeyboardUtils.showSoftInput(activity);
47+
}
48+
}
49+
50+
@Override
51+
public void onClick(View v) {
52+
switch (v.getId()) {
53+
case R.id.btn_hide_soft_input:
54+
KeyboardUtils.hideSoftInput(etInput);
55+
break;
56+
case R.id.btn_show_soft_input:
57+
KeyboardUtils.showSoftInput(etInput);
58+
break;
59+
case R.id.btn_toggle_soft_input:
60+
KeyboardUtils.toggleSoftInput();
61+
break;
62+
case R.id.btn_close_dialog:
63+
KeyboardUtils.hideSoftInput(etInput);
64+
dialog.dismiss();
65+
break;
66+
}
67+
}
68+
}

app/src/main/res/layout/activity_keyboard.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
android:layout_height="match_parent">
66

77
<LinearLayout
8+
android:id="@+id/fragment_container"
89
android:layout_width="match_parent"
910
android:layout_height="wrap_content"
1011
android:gravity="center_horizontal"
@@ -19,7 +20,7 @@
1920
android:gravity="center"/>
2021

2122
<EditText
22-
android:id="@+id/et"
23+
android:id="@+id/et_input"
2324
android:layout_width="match_parent"
2425
android:layout_height="wrap_content"
2526
android:inputType="text"/>
@@ -45,5 +46,12 @@
4546
android:layout_height="wrap_content"
4647
android:text="@string/keyboard_toggle_soft_input"/>
4748

49+
<Button
50+
android:id="@+id/btn_keyboard_in_fragment"
51+
style="@style/WideBtnStyle"
52+
android:layout_width="match_parent"
53+
android:layout_height="wrap_content"
54+
android:text="@string/keyboard_in_dialog"/>
55+
4856
</LinearLayout>
4957
</ScrollView>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ScrollView
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent">
6+
7+
<LinearLayout
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content"
10+
android:gravity="center_horizontal"
11+
android:orientation="vertical"
12+
android:padding="@dimen/spacing_16">
13+
14+
<EditText
15+
android:id="@+id/et_input"
16+
android:layout_width="match_parent"
17+
android:layout_height="wrap_content"
18+
android:inputType="text"/>
19+
20+
<Button
21+
android:id="@+id/btn_hide_soft_input"
22+
style="@style/WideBtnStyle"
23+
android:layout_width="match_parent"
24+
android:layout_height="wrap_content"
25+
android:text="@string/keyboard_hide_soft_input"/>
26+
27+
<Button
28+
android:id="@+id/btn_show_soft_input"
29+
style="@style/WideBtnStyle"
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content"
32+
android:text="@string/keyboard_show_soft_input"/>
33+
34+
<Button
35+
android:id="@+id/btn_toggle_soft_input"
36+
style="@style/WideBtnStyle"
37+
android:layout_width="match_parent"
38+
android:layout_height="wrap_content"
39+
android:text="@string/keyboard_toggle_soft_input"/>
40+
41+
<Button
42+
android:id="@+id/btn_close_dialog"
43+
style="@style/WideBtnStyle"
44+
android:layout_width="match_parent"
45+
android:layout_height="wrap_content"
46+
android:text="@string/keyboard_close_dialog"/>
47+
48+
</LinearLayout>
49+
</ScrollView>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
<string name="keyboard_hide_soft_input">Hide Soft Input</string>
8383
<string name="keyboard_show_soft_input">Show Soft Input</string>
8484
<string name="keyboard_toggle_soft_input">Toggle Soft Input</string>
85+
<string name="keyboard_in_dialog">Keyboard In Dialog</string>
86+
<string name="keyboard_close_dialog">Close Dialog</string>
8587

8688
<!-- log -->
8789
<string name="log_toggle_log">Toggle Log</string>

update_log.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* 17/06/01 完善KeyBoardUtils及Demo
12
* 17/05/30 完善CrashUtils,发布1.6.4
23
* 17/05/28 修复CacheUtils的bug,发布1.6.3
34
* 17/05/27 修复CacheUtils的bug,发布1.6.2

utilcode/src/main/java/com/blankj/utilcode/util/KeyboardUtils.java

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.util.Log;
66
import android.view.View;
77
import android.view.inputmethod.InputMethodManager;
8-
import android.widget.EditText;
98

109
/**
1110
* <pre>
@@ -27,6 +26,33 @@ private KeyboardUtils() {
2726
<p>android:windowSoftInputMode="adjustPan"</p>
2827
*/
2928

29+
/**
30+
* 动态显示软键盘
31+
*
32+
* @param activity activity
33+
*/
34+
public static void showSoftInput(Activity activity) {
35+
View view = activity.getCurrentFocus();
36+
if (view == null) view = new View(activity);
37+
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
38+
if (imm == null) return;
39+
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
40+
}
41+
42+
/**
43+
* 动态显示软键盘
44+
*
45+
* @param view 视图
46+
*/
47+
public static void showSoftInput(View view) {
48+
view.setFocusable(true);
49+
view.setFocusableInTouchMode(true);
50+
view.requestFocus();
51+
InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
52+
if (imm == null) return;
53+
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
54+
}
55+
3056
/**
3157
* 动态隐藏软键盘
3258
*
@@ -43,15 +69,23 @@ public static void hideSoftInput(Activity activity) {
4369
/**
4470
* 动态隐藏软键盘
4571
*
46-
* @param context 上下文
47-
* @param view 视图
72+
* @param view 视图
4873
*/
49-
public static void hideSoftInput(Context context, View view) {
50-
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
74+
public static void hideSoftInput(View view) {
75+
InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
5176
if (imm == null) return;
5277
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
5378
}
5479

80+
/**
81+
* 切换键盘显示与否状态
82+
*/
83+
public static void toggleSoftInput() {
84+
InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
85+
if (imm == null) return;
86+
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
87+
}
88+
5589
/**
5690
* 点击屏幕空白区域隐藏软键盘
5791
* <p>根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘</p>
@@ -89,27 +123,4 @@ private boolean isShouldHideKeyboard(View v, MotionEvent event) {
89123
}
90124
*/
91125
}
92-
93-
/**
94-
* 动态显示软键盘
95-
*
96-
* @param edit 输入框
97-
*/
98-
public static void showSoftInput(EditText edit) {
99-
edit.setFocusable(true);
100-
edit.setFocusableInTouchMode(true);
101-
edit.requestFocus();
102-
InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
103-
if (imm == null) return;
104-
imm.showSoftInput(edit, 0);
105-
}
106-
107-
/**
108-
* 切换键盘显示与否状态
109-
*/
110-
public static void toggleSoftInput() {
111-
InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
112-
if (imm == null) return;
113-
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
114-
}
115126
}

0 commit comments

Comments
 (0)