您的位置:首页 > 移动开发 > Android开发

点击空白处让输入法隐藏的比较好用的方法

2015-12-14 11:54 423 查看
//复制一下的代码直接粘贴到BaseActivity中就可以
@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {if (ev.getAction()==MotionEvent.ACTION_DOWN){View view=getCurrentFocus();if (isShouldHideInput(view,ev)){hideSoftInput(view.getWindowToken());}}return super.dispatchTouchEvent(ev);}public boolean isShouldHideInput(View view,MotionEvent event){if (view !=null&&(view instanceof EditText)){int[] pos={0,0};view.getLocationInWindow(pos);int left=pos[0];int top=pos[1];int bottom=top+view.getHeight();int right=left+view.getWidth();if (event.getX()>left&&event.getX()<right&&event.getY()>top&&event.getY()<bottom){return false;}else {return true;}}return false;}public void hideSoftInput(IBinder token){if (token!=null){InputMethodManager im= (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);im.hideSoftInputFromWindow(token,InputMethodManager.HIDE_NOT_ALWAYS);}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android 输入法