Skip to content

Commit 192c683

Browse files
author
吕发强
committed
新建软键盘工具类
1 parent cd406b8 commit 192c683

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.lvfq.library.utils;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.view.View;
6+
import android.view.inputmethod.InputMethodManager;
7+
8+
/**
9+
* LvKeyBoardUtil
10+
*
11+
* @author lvfq
12+
* @Github: https://github.com/lvfaqiang
13+
* @Blog: http://blog.csdn.net/lv_fq
14+
* @date 2017/7/26 上午10:07
15+
* @desc : 键盘工具类
16+
*/
17+
18+
public class LvKeyBoardUtil {
19+
20+
/**
21+
* 弹出软键盘
22+
*/
23+
public static void showKeyBoard() {
24+
InputMethodManager imm = (InputMethodManager) LvUtils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
25+
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
26+
}
27+
28+
/**
29+
* 弹出软键盘
30+
*
31+
* @param activity
32+
*/
33+
public static void showKeyBoard(Activity activity) {
34+
View view = activity.getCurrentFocus();
35+
if (view == null) view = new View(activity);
36+
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
37+
if (imm == null) return;
38+
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
39+
}
40+
41+
/**
42+
* 弹出软键盘
43+
*
44+
* @param view
45+
*/
46+
public static void showKeyBoard(View view) {
47+
view.setFocusable(true);
48+
view.setFocusableInTouchMode(true);
49+
view.requestFocus();
50+
InputMethodManager imm = (InputMethodManager) LvUtils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
51+
if (imm == null) return;
52+
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
53+
}
54+
55+
56+
/**
57+
* 隐藏软件盘
58+
*
59+
* @param view
60+
*/
61+
public static void hideKeyBoard(View view) {
62+
InputMethodManager imm = (InputMethodManager) LvUtils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
63+
if (imm == null) return;
64+
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
65+
}
66+
67+
/**
68+
* 隐藏软键盘
69+
*
70+
* @param activity
71+
*/
72+
public static void hideKeyBoard(Activity activity) {
73+
View view = activity.getCurrentFocus();
74+
if (view == null) view = new View(activity);
75+
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
76+
if (imm == null) return;
77+
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
78+
}
79+
80+
}

0 commit comments

Comments
 (0)