Skip to content

Commit

Permalink
feat : edittext 영역 외 클릭 시 키보드 자동 내림 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
m6z1 committed Feb 5, 2024
1 parent 226b4ad commit f10fee0
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dongyang.android.youdongknowme.ui.view.search

import android.annotation.SuppressLint
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
Expand All @@ -20,9 +21,14 @@ class SearchFragment : BaseFragment<FragmentSearchBinding, SearchViewModel>() {
}

override fun initDataBinding() {
setupUI()
setTextClearButtonClickListener()
}

private fun setupUI() {
showKeyboardOnEditTextFocus()
setupHideKeyboardOnOutsideTouch()
setTextClearButtonVisibility()
setTextClearButtonClickListener()
}

private fun showKeyboardOnEditTextFocus() {
Expand All @@ -38,6 +44,20 @@ class SearchFragment : BaseFragment<FragmentSearchBinding, SearchViewModel>() {
inputMethodManager.showSoftInput(binding.etSearchBar, InputMethodManager.SHOW_IMPLICIT)
}

@SuppressLint("ClickableViewAccessibility")
private fun setupHideKeyboardOnOutsideTouch() {
binding.root.setOnTouchListener { _, _ ->
hideKeyboard()
false
}
}

private fun hideKeyboard() {
val inputMethodManager =
requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view?.windowToken, 0)
}

private fun setTextClearButtonVisibility() {
binding.etSearchBar.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
Expand Down

0 comments on commit f10fee0

Please sign in to comment.