In the Android development process, sometimes we have the need to force off the soft keyboard. For example: Now there is a text edit box (testet) and a button (TESTBTN), we now click on the text edit box tested, this will pop up the soft keyboard, then we click on the button testbtn, the soft keyboard still remains open state ... The problem comes, and the result we want is the soft keyboard disappears. (Testbtn is just a random example of me, you can also make other controls such as drop-down boxes, clickable pictures, custom spaces, etc.)
Here are two ways to solve:
First, this method just turns off the soft keyboard:
In the button testbtn call the following method Hidekeyboard ():
private void Hidekeyboard () {
Inputmethodmanager IMM = (inputmethodmanager) this.getsystemservice (Context.input_method_service);
if (imm.isactive () && this.getcurrentfocus () = null) {
if (This.getcurrentfocus (). Getwindowtoken () = null) {
Imm.hidesoftinputfromwindow (This.getcurrentfocus (). Getwindowtoken (), inputmethodmanager.hide_not_always);
}
}
}
Second, this method will determine the state of the soft keyboard, if the closed state is open, if it is open the state is closed:
In the button testbtn call the following method Hidekeyboard ():
private void Hidekeyboard () {
Inputmethodmanager IMM = (inputmethodmanager) this.getsystemservice (Context.input_method_service);
if (imm.isactive ()) {
if (This.getcurrentfocus (). Getwindowtoken () = null) {
Imm.hidesoftinputfromwindow (This.getcurrentfocus (). Getwindowtoken (), inputmethodmanager.hide_not_always);
}
}
}
Android Force soft Keyboard off