I am new to Android development and having a problem with what SHOULD be a very simple task. I want to receive KeyEvents whenever a user is typing in an EditText field because I want to save their entered values to data structures in the background on each key stroke.
I have mimic’d the code in the Beginner’s Dev guide at http://developer.android.com/resources/tutorials/views/hello-formstuff.html#EditText and set up an OnKeyListener. Here is a snippet of my code:
cell.amountEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
System.err.println("onKey for Amount, key="+event.getDisplayLabel());
if (event.getAction() == KeyEvent.ACTION_DOWN) {
return onKeyDownInAmount(finalPosition, (EditText)v, keyCode, event);
} else {
return false;
}
}
});
Behavior on the emulator is spotty at best, some times it will deliver the KeyEvents for the virtual keyboard, sometimes it won’t. When I install the app on my device (HTC Hero which has a virtual keyboard only) then NONE of the events fire. I never receive a single KeyEvent.
What am I doing wrong?
Any help is appreciated.
The onKeyListener only receives events from a hardware keyboard. Use TextWatcher instead.