EditText를 누르면 외부에서 초점을 잃고 내부를 누르면 자동으로 초점을 가져옵니다

3510 단어 Android
        baseActivity        editText    
// editText          
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        View v = getCurrentFocus();
        if (isShouldHideInput(v, ev)) {//  editText    
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                assert v != null;
                hideInput();
                if (editText != null) {
                    editText.clearFocus();
                }
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    //     ,           TouchEvent 
    return getWindow().superDispatchTouchEvent(ev) || onTouchEvent(ev);
}

EditText editText = null;

public boolean isShouldHideInput(View v, MotionEvent event) {
    if (v != null && (v instanceof EditText)) {
        editText = (EditText) v;
        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();
        return !(event.getX() > left && event.getX() < right
                && event.getY() > top && event.getY() < bottom);
    }
    return false;
}

좋은 웹페이지 즐겨찾기