Handling virtual keyboard on an EditText
I'm running into trouble getting the key events on an EditText
with the virtual keyboard. The user is p开发者_如何转开发rompted to type something and i need to capture those events as they time. I need to be able to get the keyup
and keydown
. I need to get the timing of the ups and downs. this is designed for 3.1 Is there any way to get these events? Any help would be appreciated
Thanks, Michael
Android reference says
A key press starts with a key event with ACTION_DOWN. If the key is held sufficiently long that it repeats, then the initial down is followed additional key events with ACTION_DOWN and a non-zero value for getRepeatCount(). The last key event is a ACTION_UP for the key up. If the key press is canceled, the key up event will have the FLAG_CANCELED flag set.
Please, try something like this:
public void onCreate(Bundle savedInstanceState) {
...
etField =(EditText)findViewById(R.id.your_edit_text);
etField.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// register the text when "enter" is pressed
if (event.getAction() == KeyEvent.ACTION_DOWN
|| event.getAction() == KeyEvent.ACTION_UP ) {
int timesPressed = event.getRepeatCount();
//do something
}
});
}
精彩评论