Android keyboard automatically hides and transfers focus

Source: Internet
Author: User

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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.