Android realizes clicking on the blank space to recycle the keyboard

1 Click Blank to Recycle Keyboard Ideas

First of all, it must be determined whether the position where the screen is clicked is a blank space. If it is not a blank space, there is no need to retract the keyboard.
1 How to determine whether it is a blank space or an input box? It is necessary to monitor the touched position. If the touched position is in the input box, there is no need to recycle. Otherwise, if the touched position is outside the input box, you need to recycle the keyboard

public static boolean isShouldHideKeyBord(View v, MotionEvent ev) {
    
    
        if (v != null && (v instanceof EditText)) {
    
    
            int[] l = {
    
    0, 0};
            //获取到当前窗口的父窗口坐标
            v.getLocationInWindow(l);
            int left = l[0];
            int top = l[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            return !(ev.getX() > left && ev.getX() < right && ev.getY() > top && ev.getY() < bottom);
        }
        return false;
    }

2 How to recycle the keyboard, as follows

public static void hintKeyBoards(View view) {
    
    
        InputMethodManager manager = ((InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE));
        if (manager != null) {
    
    
            manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

Summary: Encapsulate the above two steps into a tool class, and directly add it to the corresponding interface when using it

public class KeyboardUtils {
    
    
    //展示键盘的方法
    public static void showKeyboard(View view) {
    
    
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
    
    
            view.requestFocus();
            imm.showSoftInput(view, 0);
        }
    }

    /**
     * 隐藏软键盘
     * @param view
     */
    public static void hintKeyBoards(View view) {
    
    
        InputMethodManager manager = ((InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE));
        if (manager != null) {
    
    
            manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 判定当前是否需要隐藏
     */
    public static boolean isShouldHideKeyBord(View v, MotionEvent ev) {
    
    
        if (v != null && (v instanceof EditText)) {
    
    
            int[] l = {
    
    0, 0};
            //获取到当前窗口的父窗口坐标
            v.getLocationInWindow(l);
            int left = l[0];
            int top = l[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            return !(ev.getX() > left && ev.getX() < right && ev.getY() > top && ev.getY() < bottom);
        }
        return false;
    }
}

2Kotlin usage method, just add the following method directly to the required interface

		@CallSuper
        override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
    
    
                if (ev.action == MotionEvent.ACTION_DOWN) {
    
    
                        val view = currentFocus
                        //打印出x和y的坐标位置
                        if (KeyboardUtils.isShouldHideKeyBord(view, ev)) {
    
    
                                KeyboardUtils.hintKeyBoards(view)
                        }
                }
                return super.dispatchTouchEvent(ev)
        }

Guess you like

Origin blog.csdn.net/m0_56184347/article/details/125608407