about_keyboard.md 4.1 KB
Newer Older
B
blankj 已提交
1 2
# 键盘相关
### 避免输入法面板遮挡
B
blankj 已提交
3
``` java
B
blankj 已提交
4
// 在manifest.xml中activity中设置
B
blankj 已提交
5 6 7
android:windowSoftInputMode="stateVisible|adjustResize"
```

B
blankj 已提交
8
### 动态隐藏软键盘
B
blankj 已提交
9 10
``` java
/**
B
blankj 已提交
11
* 动态隐藏软键盘
B
blankj 已提交
12 13 14 15 16 17 18 19 20 21 22
*/
public static void hideSoftInput(Activity activity) {
    View view = activity.getWindow().peekDecorView();
    if (view != null) {
        InputMethodManager inputmanger = (InputMethodManager) activity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

/**
B
blankj 已提交
23
* 动态隐藏软键盘
B
blankj 已提交
24 25 26 27 28 29 30 31 32
*/
public static void hideSoftInput(Context context, EditText edit) {
    edit.clearFocus();
    InputMethodManager inputmanger = (InputMethodManager) context
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    inputmanger.hideSoftInputFromWindow(edit.getWindowToken(), 0);
}
```

B
blankj 已提交
33
###  点击屏幕空白区域隐藏软键盘
B
blankj 已提交
34 35
``` java
/**
36 37 38
 * 点击屏幕空白区域隐藏软键盘(方法0)
 * <p>在onTouch中处理,未获焦点则隐藏
 * <p>参照以下注释代码
39 40 41 42 43 44 45 46 47
 */
public static void clickBlankArea2HideSoftInput0() {
    Log.i("tips", "U should copy the following code.");
    /*
    @Override
    public boolean onTouchEvent (MotionEvent event){
        if (null != this.getCurrentFocus()) {
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
B
blankj 已提交
48
        }
49
        return super.onTouchEvent(event);
B
blankj 已提交
50
    }
51
    */
B
blankj 已提交
52 53 54
}

/**
55 56 57 58
 * 点击屏幕空白区域隐藏软键盘(方法1)
 * <p>根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘
 * <p>需重写dispatchTouchEvent
 * <p>参照以下注释代码
59 60 61 62 63 64 65 66 67 68 69 70 71
 */
public static void clickBlankArea2HideSoftInput1() {
    Log.i("tips", "U should copy the following code.");
    /*
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, ev)) {
                hideKeyboard(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
B
blankj 已提交
72
    }
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    // 根据EditText所在坐标和用户点击的坐标相对比,来判断是否隐藏键盘
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            return !(event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom);
        }
        return false;
    }
    // 获取InputMethodManager,隐藏软键盘
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
B
blankj 已提交
93
    }
94
    */
B
blankj 已提交
95 96 97
}
```

B
blankj 已提交
98
### 动态显示软键盘
B
blankj 已提交
99 100
``` java
/**
B
blankj 已提交
101
* 动态显示软键盘
B
blankj 已提交
102 103 104 105 106 107 108 109 110 111 112
*/
public static void showSoftInput(Context context, EditText edit) {
    edit.setFocusable(true);
    edit.setFocusableInTouchMode(true);
    edit.requestFocus();
    InputMethodManager inputManager = (InputMethodManager) context
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.showSoftInput(edit, 0);
}
```

B
blankj 已提交
113
### 切换键盘显示与否状态
B
blankj 已提交
114 115
``` java
/**
B
blankj 已提交
116
* 切换键盘显示与否状态
B
blankj 已提交
117 118 119 120 121 122 123 124 125 126
*/
public static void toggleSoftInput(Context context, EditText edit) {
    edit.setFocusable(true);
    edit.setFocusableInTouchMode(true);
    edit.requestFocus();
    InputMethodManager inputManager = (InputMethodManager) context
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
```