public class MainActivity extends AppCompatActivity {
...
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
View focusView = getCurrentFocus();
if (focusView != null) {
Rect rect = new Rect();
focusView.getGlobalVisibleRect(rect);
int x = (int) ev.getX(), y = (int) ev.getY();
if (!rect.contains(x, y)) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imm != null)
imm.hideSoftInputFromWindow(focusView.getWindowToken(), 0);
focusView.clearFocus();
}
}
return super.dispatchTouchEvent(ev);
}
...
현재 Focus를 받고 있는 View의 영역이 아닌 다른 곳에 터치 이벤트가 일어나면 InputMethodManager을 통해 키보드를 내리는 코드입니다.
TouchEvent의 흐름에 대해 궁금하신 분들은 아래 StackOverFlow의 글을 참고해주세요.
https://stackoverflow.com/questions/7449799/how-are-android-touch-events-delivered
How are Android touch events delivered?
I'm not asking how to handle touch events, but what is going on behind the scenes? If there are several nested widgets, what order do they see the events in? Does the developer have any control ove...
stackoverflow.com
'Android' 카테고리의 다른 글
[Android] 앱 Background로 보내기 (0) | 2020.02.07 |
---|---|
[Android] onMeasure 함수의 인자들 (0) | 2020.02.05 |
[Android] Activity Stack, Task 확인하기 (0) | 2020.01.31 |
[Android] Task와 Task Affinity에 대한 나의 이해 (0) | 2020.01.29 |
[Android] Android Dagger2 연습 중 떴던 오류 (0) | 2020.01.28 |