반응형
안드로이드 디바이스의 소프트 키보드를 내리려면 아래 와 같은 소스를 extension으로 등록해 놓으면 편합니다.
import android.app.Activity
import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}
fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
HideExtension.kt 파일로 생성 후 위 소스를 넣으면 됩니다.
사용 방법은
// 특정 뷰의 키보드를 내릴때
baseContext.hideKeyboard(view)
// 액티비티에 먹은 뷰를 내릴때
this@Activity.hideKeyboard()
위와 같이 사용 하면 됩니다.
'Kotlin programming > 코틀린(kotlin)' 카테고리의 다른 글
[Kotlin] txt 파일 생성하고 데이터 내보내기(export txt file) (0) | 2022.02.15 |
---|---|
[Kotlin][Naver login 구현] 앱이 설치돼있지 않을 때 로그인 시도 시 발생하는 에러 (0) | 2022.01.28 |
[Kotlin] JCenter 서비스 업데이트로 인한 mavenCentral 전환 이슈(Migration Jcenter to mavenCentral) (0) | 2022.01.18 |
[Kotlin] ONE UI 4.0 업데이트 하고 나서 앱이 사라 졌다! ㅠㅠ (0) | 2022.01.04 |
AsyncTask로 되어 있는 소스 Coroutine로 바꾸기(Change AsyncTask to Coroutine) (0) | 2021.12.31 |