1. In Android development, if you encounter a requirement today, if you click another control when entering text, the keyboard operation is automatically hidden and the Click Event of the control is not executed,
The next click after the hidden Keyboard can trigger the click event. I have found a lot on the Internet, but all tests fail later.
The following are the results of my personal trial,CodeAs follows:
Note: otherview is another component that can be any control. It only transfers the focus from the input box. input_edit is a textedit. When the input is in progress,
When you click an event other than the keyboard, such as onclick, the keyboard is automatically hidden.
Public Boolean checkkeyboardshowing () {otherview. requestfocus (); inputmethodmanager Imm = (inputmethodmanager) This. getsystemservice (context. input_method_service); Boolean active = Imm. isactive (input_edit); Imm. hidesoftinputfromwindow (input_edit.getwindowtoken (), inputmethodmanager. hide_not_always); input_edit.clearfocus (); Return active ;}
Resolution:
When calling otherview. requestfouces (), the focus is first transferred to any other control, which is extremely important.
Imm. isactive (input_edit); returns whether to enable the keyboard. Note: if the focus is not transferred in the previous step, the value is always true (that is, true even if the keyboard is disabled ).
Imm. hidesoftinputfromwindow (...,...); disable the keyboard
Input_edit.clearfocus (). Of course, the focus is cleared.
Determine whether the keyboard is displayed in the event and whether the event is executed:
Public void onclick (view v) {Boolean isshowkeyboard = g_context.checkkeyboardshowing (); If (isshowkeyboard) return; If (v. GETID () = R. id. BTN ){//......}}
2. In addition, set the focus for a textedit through the trigger event and pop up the keyboard as follows:
Merge (); inputmethodmanager Imm = (inputmethodmanager) input_edit.getcontext (). getsystemservice (context. input_method_service); Imm. togglesoftinput (0, inputmethodmanager. show_forced );