Android 点击editview以外位置实现隐藏输入法
实现代码:
@Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { View v = getActivity().getCurrentFocus(); if (isShouldHideInput(v, ev)) { InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } } return getActivity().dispatchTouchEvent(ev); } // 必不可少,否则所有的组件都不会有TouchEvent了 if (getActivity().getWindow().superDispatchTouchEvent(ev)) { return true; } return getActivity().onTouchEvent(ev); } public 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()top && event.getY()
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!