There is a method that hides the keyboard

private fun hideKeyboard() { val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm?.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS) } 

It works, but toggleSoftInput() always works when you click on an element. It hides, it shows the keyboard, and I only need to hide. How can I modify the method? So I understand how it is using iBinder'a ?

    1 answer 1

    Try this method:

     private fun hideKeyboard(View view) { val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm?.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS, null) } 

    The hideSoftInputFromWindow() method can accept 3 parameters as input

    Free translation site https://developer.android.com/

    • there are three parameters '(IBinder windowToken, int flags, ResultReceiver resultReceiver)' - Morozov
    • one
      @Morozov in the java implementation, this method can have 2 parameters, namely: (IBinder windowToken, int flags) , since it took the example of java, it turned out to be such an inaccuracy. Thanks for the comment, corrected the answer. - zTrap
    • @zTrap 0 emphasizes - the integer literal doesn t conform to the expected IBinder! - Inkognito
    • @Inkognito try passing to the View method that was clicked. And in the method instead of 0, substitute view.getWindowToken() - zTrap
    • @zTrap Yes, I did, just compile for a long time), but why do I need getWindowToken and how does it affect the view? - Inkognito