When my user press Enter on the virtual android \"user validate entry!\" keyboard my keyboard stay visible! (Why?)
Here my Java code...
privat
Keep the singleLine="true" and add imeOptions="actionDone" to the EditText. Then in the OnEditorActionListener check if actionId == EditorInfo.IME_ACTION_DONE, like so (but change it to your implementation):
if (actionId == EditorInfo.IME_ACTION_DONE) {
if ((username.getText().toString().length() > 0)
&& (password.getText().toString().length() > 0)) {
// Perform action on key press
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(username.getWindowToken(),
0);
doLogin();
}
}
I am create a custom component who extends AutoCompleteTextView, like in the example below:
public class PortugueseCompleteTextView extends AutoCompleteTextView {
...
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_BACK)) {
InputMethodManager inputManager =
(InputMethodManager) getContext().
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(
this.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
return super.onKeyPreIme(keyCode, event);
}
I am using this code in the AlertDialog.Builder, but is possible to be using to Activity.
This should do it:
yourEditTextHere.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// NOTE: In the author's example, he uses an identifier
// called searchBar. If setting this code on your EditText
// then use v.getWindowToken() as a reference to your
// EditText is passed into this callback as a TextView
in.hideSoftInputFromWindow(searchBar
.getApplicationWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
userValidateEntry();
// Must return true here to consume event
return true;
}
return false;
}
});
just add this line in your edit text.
android:imeOptions="actionDone"'
you can specify the next edit text id to move to that edit text on click of the keyboard done button.
If you make the text box a single line (I believe the propery is called SingleLine in the layout xml files) it will exit out of the keyboard on enter.
Here you go: http://developer.android.com/reference/android/R.styleable.html#TextView_singleLine