whyclj
2019-12-31 d2272f21a9d4491f22b3d3c9d6dbfebcecff76e3
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
package com.mytestapp;
 
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
 
/**
 * 软键盘工具类
 *   ·是否隐藏
 *   ·显示
 *   ·隐藏
 */
public class SoftInputUtil {
 
    /**
     *  判断键盘是否显示(当前editText是否在输入法活动状态,即是否正在接收软键盘输入)。
     * @param editText 输入框
     */
    public static boolean isSoftInputShowing(EditText editText){
        return isSoftInputShowing(editText.getContext(), editText);
 
    }
 
    /**
     * 判断键盘是否显示(当前view是否在输入法活动状态,即是否正在接收软键盘输入)。
     * @param context 上下文
     * @param view 当前聚焦的正在接收软件盘输入的View。
     */
    public static boolean isSoftInputShowing(Context context, View view) {
        boolean bool = false;
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && imm.isActive(view)) {
            bool = true;
        }
        return bool;
    }
 
    /**
     * 判断键盘是否显示(是否有View是在输入法活动状态,即是否正有View正在接收软键盘输入)。
     * @param context 上下文
     */
    public static boolean isSoftInputShowing(Context context) {
        boolean bool = false;
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && imm.isActive()) {
            bool = true;
        }
        return bool;
    }
 
    /**
     * 显示键盘
     * @param view 当前聚焦的正在接收软件盘输入的View,通常为EditText。
     */
    public static void showSoftInput(View view) {
        showSoftInput(view.getContext(), view);
    }
 
    /**
     * 显示键盘
     * @param context 上下文
     * @param view 当前聚焦的正在接收软件盘输入的View,通常为EditText。
     */
    public static void showSoftInput(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            //第二个参数为flags, 0 | InputMethodManager.SHOW_FORCED | InputMethodManager.SHOW_IMPLICIT
            imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
        }
    }
 
    /**
     * 隐藏软键盘
     * @param activity
     */
    public static void hideSoftInput(Activity activity) {
        View view = activity.getWindow().peekDecorView();
        if (view != null) {
            hideSoftInput(activity, view);
        }
    }
 
    public static void hideSoftInput(View view) {
        if (view != null) {
            hideSoftInput(view.getContext(), view);
        }
    }
 
    /**
     * 隐藏输入法
     * @param context 上下文
     * @param view 可以是任意已添加到window中的View(已添加到布局中的View)。
     */
    public static void hideSoftInput(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && imm.isActive()) {
            //第二个参数为flags, 0 | InputMethodManager.HIDE_IMPLICIT_ONLY | InputMethodManager.HIDE_NOT_ALWAYS
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
 
}