//复制一下的代码直接粘贴到BaseActivity中就可以
@Override
public 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);
}
}