There was a slight quirk that I ran into while working on my app. If I were editing text and clicked "Save" on the activity, it'd close the screen but keep the keyboard open.
I'd then have to press the BACK button to close it. A small but somewhat irritating quirk. Luckily, its an easy thing to fix.
First, define an "OnFocusChangeListener":
01.
private
OnFocusChangeListener hideKeyboard =
new
OnFocusChangeListener() {
02.
@Override
03.
public
void
onFocusChange(View v,
boolean
hasFocus) {
04.
if
(!hasFocus) {
05.
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
06.
imm.hideSoftInputFromWindow(v.getWindowToken(),
0
);
07.
}
08.
}
09.
};
When you're creating and setting up your edit boxes, just set the following listeners:
1.
txtEmail.setOnFocusChangeListener(hideKeyboard);
2.
txtUsername.setOnFocusChangeListener(hideKeyboard);
There you have it. It should now hide the keyboard automatically.
If no other objects take focus, then just call the listener manually.
1.
hideKeyboard.onFocusChange(txtUsername,
false
);
2.
hideKeyboard.onFocusChange(txtEmail,
false
);
[ Source ]