When my user press Enter on the virtual android "user validate entry!" keyboard my keyboard stay visible! (Why?)
Here my Java code...
private void initTextField() {
entryUser = (EditText) findViewById(R.id.studentEntrySalary);
entryUser.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
userValidateEntry();
return true;
}
}
return true;
}
});
}
private void userValidateEntry() {
System.out.println("user validate entry!");
}
... here my View
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content">
<EditText android:id="@+id/studentEntrySalary" android:text="Foo" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
Maybe something wrong on my virtual device?
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;
}
});