프로젝트를 진행하다보면 화면이 전환되자마자 키보드는 올리거나
또는 어떤 특정한 상황에서 키보드를 내려야 할 때가 있다.
오늘은 어떻게 키보드를 내리고 올릴 수 있는지 알아보자❗❗
일반적으로 Util 패키지에 따로 클래스를 생성하여 전체 Activity 혹은 Fragment 에서 활용할 수 있도록
만들어 놓는 편
키보드 내리기
fun hideSoftInput(context: Context, editText: EditText) {
val inputMethodManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(editText.windowToken, 0)
}
키보드 올리기
fun showSoftInput(context: Context, editText: EditText) {
val inputMethodManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(editText, 0)
}
InputMethodManager❓
하드웨어 키보드는 스마트폰에서 물리적으로 제공하는 키보드로 우리의 앱에서 창을 제어하는 것과 관련이 기능을 제공하는 클래스
📛 주의할 점
- showSoftInput ( ) 함수의 첫 번째 매개변수로 글이 입력될 뷰를 지칭하는데, 입력 대상이 되는 뷰에 포커스가 없는 상태라면 키보드가 나타나지 않음
- 현재 포커스가 없는 EditText에 입력되게 키보드를 보이려면 requestFocus ( ) 함수를 이용하여 포커스를 요청한 후 showSoftInput( ) 함수로 키보드가 나타나게 하면 됨
참고
반응형
'개발 > Android' 카테고리의 다른 글
[Android] CustomView 개념 및 사용법 (0) | 2023.01.13 |
---|---|
[Android] Android API Level (0) | 2023.01.10 |
[Android] AppBar custom 시 Background 색상 나오는 문제 (0) | 2022.08.28 |
[Android] TMAP POI 사용기2 (0) | 2022.08.24 |
[Android] Google Map Key 오류 (0) | 2022.08.21 |
댓글