Advertisement
leptodon

hideKeyboardOnFocusChange

Dec 29th, 2020
1,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.78 KB | None | 0 0
  1. /*
  2. Make the parent view(content view of your activity) clickable and focusable by adding the following attributes
  3.  
  4.     android:clickable="true"
  5.     android:focusableInTouchMode="true"
  6. */
  7.  
  8. class MyClass {
  9.     private var editText: View
  10.  
  11.     init {
  12.         editText = findViewById(R.id.editText)
  13.     }
  14.    
  15.     editText.onFocusChangeListener = View.OnFocusChangeListener
  16.     {
  17.         v, hasFocus ->
  18.         if (!hasFocus) {
  19.             hideKeyboard(v)
  20.         }
  21.     }
  22.  
  23. //    Функцию hideKeyboard() можно вынести в Util класс
  24.     private fun hideKeyboard(view: View) {
  25.         val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
  26.         inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement