I have an EditText that I want to respond to a user pressing the "Enter" key. Here is the relevant code:
EditText edittext = (EditText) findViewById(R.id.user_query);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER)) {
sendMessage(v);
return true;
}
return false;
}
});
When I run this on my tablet (Toshiba Thrive), it works perfectly. However, on my phone (HTC Desire HD), the OnKeyListener doesn't fire at all when the "Enter" key is pressed... but works when "Enter" is pressed a second time. Why is that, and how can I fix it?
With autocorrect on the phone, pressing "Enter" once selects the suggested spelling of the word. Pressing "Enter" the second time sends the key press to the app. It's a feature, not a bug.
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