Android--点击视图外部,隐藏键盘

来源:互联网 发布:iphone7plus在线软件 编辑:程序博客网 时间:2024/05/01 07:29

来源:http://stackoverflow.com/questions/4828636/edittext-clear-focus-on-touch-outside

//这个方法在从一个输入框点到另一个输入框时,会造成输入框先缩入再显示的问题,(即跳动)
需要再加一个判断。。

    //    @Override    public boolean dispatchTouchEvent(MotionEvent event) {        if (event.getAction() == MotionEvent.ACTION_DOWN) {            View v = getCurrentFocus();            if ( v instanceof SearchView.SearchAutoComplete) {                Rect outRect = new Rect();                v.getGlobalVisibleRect(outRect);                if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {                    v.clearFocus();                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);                }            }        }        return super.dispatchTouchEvent( event );    }
0 0
原创粉丝点击