android 自定义点击非EditText区域自动隐藏软键盘
public class AutoHideInputMethodFrameLayout extends FrameLayout {public AutoHideInputMethodFrameLayout(Context context) {super(context);}public AutoHideInputMethodFrameLayout(Context context, Attribut
·
public class AutoHideInputMethodFrameLayout extends FrameLayout { public AutoHideInputMethodFrameLayout(Context context) { super(context); } public AutoHideInputMethodFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); } public AutoHideInputMethodFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { Context context = getContext(); if (context == null || !(context instanceof Activity)) { return super.dispatchTouchEvent(ev); } Activity activity = (Activity) context; View focusView = activity.getCurrentFocus(); if (focusView != null && shouldHideInputMethod(focusView, ev)) { hideInputMethod(activity); } } return super.dispatchTouchEvent(ev); } private boolean shouldHideInputMethod(View focusView, MotionEvent event) { Rect rect = new Rect(); focusView.getHitRect(rect); return !rect.contains((int) event.getX(), (int) event.getY()); } private void hideInputMethod(Activity activity) { if (activity.getCurrentFocus() != null) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } }
xml布局包裹对应的最外层布局即可....只可父子关系
更多推荐
所有评论(0)