Android 点击editview以外位置实现隐藏输入法
Android点击editview以外位置实现隐藏输入法
实现代码:
@Override
publicbooleandispatchTouchEvent(MotionEventev){
if(ev.getAction()==MotionEvent.ACTION_DOWN){
Viewv=getActivity().getCurrentFocus();
if(isShouldHideInput(v,ev)){
InputMethodManagerimm=(InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm!=null){
imm.hideSoftInputFromWindow(v.getWindowToken(),0);
}
}
returngetActivity().dispatchTouchEvent(ev);
}
//必不可少,否则所有的组件都不会有TouchEvent了
if(getActivity().getWindow().superDispatchTouchEvent(ev)){
returntrue;
}
returngetActivity().onTouchEvent(ev);
}
publicbooleanisShouldHideInput(Viewv,MotionEventevent){
if(v!=null&&(vinstanceofEditText)){
int[]leftTop={0,0};
//获取输入框当前的location位置
v.getLocationInWindow(leftTop);
intleft=leftTop[0];
inttop=leftTop[1];
intbottom=top+v.getHeight();
intright=left+v.getWidth();
if(event.getX()>left&&event.getX()top&&event.getY()
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!