키보드 이외의 영역 터치시 키보드 숨기기
📄 MainActivity.kt
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
hideKeyboard()
return super.dispatchTouchEvent(ev)
}
private fun hideKeyboard(){
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
키보드 버튼에 액션 달아주기
📄 activity_main.xml
<EditText>
android:id="@+id/edt_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- 사용하고자 하는 액션으로 설정 (ex. Done .. ) -->
android:imeOptions="actionSearch"
<!-- 개행금지 속성(singleLine deprecated) 필수X -->
android:inputType="text"
android:maxLines="1"
</EditText>
📄 MainActivity.kt
binding.edtText.setOnEditorActionListener { v, actionId, event ->
var handled = false
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(binding.edtText.windowToken,0)
// 리스트뷰에 검색 결과를 뿌려주는 함수
setSearchListAdapter()
handled = true
}
handled
}