您的位置:首页 > 其它

EditText失去焦点隐藏软键盘

2018-02-28 17:23 141 查看
1, 页面根布局加android:focusable="true"android:focusableInTouchMode="true"
2.在activity里面重写 dispatchTouchEvent@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {if (ev.getAction() == MotionEvent.ACTION_DOWN) {View v = getCurrentFocus();if (ViewUtil.isShouldHideInput( v, ev )) {InputMethodManager imm = (InputMethodManager) getSystemService( Context.INPUT_METHOD_SERVICE);if (imm != null) {imm.hideSoftInputFromWindow(v.getWindowToken(), 0);}}return super.dispatchTouchEvent( ev );}// 必不可少,否则所有的组件都不会有TouchEvent了if (getWindow().superDispatchTouchEvent( ev )) {return true;}return onTouchEvent( ev );}

/*** 是否隐藏键盘* @param v* @param event* @return*/public static boolean isShouldHideInput(View v, MotionEvent event) {if (v != null && (v instanceof EditText)) {int[] leftTop = {0, 0};//获取输入框当前的location位置v.getLocationInWindow( leftTop );int left = leftTop[0];int top = leftTop[1];int bottom = top + v.getHeight();int right = left + v.getWidth();if (event.getX() > left && event.getX() < right&& event.getY() > top && event.getY() < bottom) {// 点击的是输入框区域,保留点击EditText的事件return false;} else {return true;}}return false;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐