简体   繁体   中英

Dismiss Soft Keyboard after Hardware Search is performed

I have an Activity which performs searching within my app. I have this Activity set as the handler for the hardware search button. This all works great.

If a user navigates to my Activity, enters a search query and then clicks on the "Search" button then I dismiss the soft keyboard via:

    InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(etQuery.getWindowToken(), 0);   

etQuery is my EditText field.

However, if a user taps the hardware search button, then the search input overlap is shown, and the user is allowed to enter text, which they do, upon clicking "Go" my Activity gets the input and performs the search successfully. However, the soft keyboard is still visible.

Even though I do call the above keyboard dismissal code, it doesnt actually work. I assume because the window [token] that initiated the soft keyboard is NOT in fact the etQuery EditText - it was initiated by the Hardware Search facility. Thus asking it to close based on the EditTexts token has no effect.

At this point, I don't really care who opened the soft keyboard, I just want it to close.

How can I force the soft keyboard to close regardless of who opened it?

I solved this by adding the following attribute to the activity in the manifest:

android:windowSoftInputMode="stateHidden"

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