简体   繁体   中英

Perform action when soft keyboard is closing

I want to perform a method when the soft keyboard is closing. I tried overriding the onBackPressed() method but it didn't work because the back-button changes when the soft keyboard is open (below is a screenshot of the button).

Hide-Soft-Keyboard-Button

I tried using the OnKeyListener but it turned out that it doesn't work with the soft keyboard. And I also don't know the keyCode of this "hide-soft-keyboard-button" either...

I would appreciate any help since couldn't find a solution anywhere.

You could use a layout listener to see if the root layout of your activity is resized by the keyboard but detecting whether the softkeyboard is shown or has disappeared is only possible with some workarounds and hacks, hence i would not recommend it.

It would be more appropriate to set a Focus change listener on your edittext :

yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
 public void onFocusChange(View v, boolean hasFocus) {
    if (hasFocus) {
        //got focus
    } else {
        //lost focus
    }
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM