I've noticed in the Android Market Application
, when you click over the search button, it shows the keyboard, but when you click the back
button, the search EditText
becomes invisible and the keyboard
is hidden. The problem is that I can't hide the EditText
after the keyboard is hidden after pressing the back key because I can't find a listener for hiding the keyboard event.
I found this sample How to capture the "virtual keyboard show/hide" event in Android?
but it doesn't work on the soft keyboard.
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.
To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.
You need to implement this to capture the BACK button before it is dispatched to the IME:
http://developer.android.com/reference/android/view/View.html#onKeyPreIme(int, android.view.KeyEvent)
I think you should handle this using focus:
final InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
edttext.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!(hasFocus))
{
mgr.hideSoftInputFromWindow(edttext.getWindowToken(), 0);
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With