How to recognize keystrokes?

I know how to listen when the ENTER button in a TextView is pressed, as shown in the code below:

textView.setOnKeyListener(new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { enterPressed(); return true; } return false; } }); 

However ... how do I listen when a key with a character is pressed (AZ, 0-9, special characters, etc.), basically everything else except ENTER, BACKSPACE or SPACE? I want to do this because I want the button to turn on when the user starts typing in the TextView. Oddly enough, the onKey () method is not even called when these character keys are pressed, is there any other way I should listen to them? Thanks in advance!

+4
source share
3 answers

Text observer can help you

 textView.addTextChangedListener(new TextWatcher(){ @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } }); 
+5
source

"The keys on the software keyboard do not usually run this method, although some may choose this in some situations. Do not assume that the software input method should be based on the keys, even if it is, it may use the key; otherwise, than you expect, so there's no way to reliably pick up soft keystrokes for the enter keys. "

http://developer.android.com/reference/android/view/View.OnKeyListener.html

+1
source

you will need to write a function something like isCharacter (int). Pass the KeyEvent key code to this function, check what the range of int is in the range of characters that you want for alphabets and numbers, if so the hanlde function is pressed in returnTrue in you case or false return from isCharacter ...

0
source

All Articles