I found several examples on this subject, somewhere with a switch somewhere with c if, but that is not the point, and so it does not work. The handler responds only to pressing numbers. Even the default switch `e does not respond. What am I doing wrong? Here is the code

edit.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (KeyEvent.ACTION_DOWN == event.getAction()){ switch (keyCode){ case KeyEvent.KEYCODE_ENTER: Log.e("TAG", "Нажат интер"); break; case KeyEvent.KEYCODE_2: Log.e("TAG", "Нажато 2"); break; case KeyEvent.KEYCODE_E: Log.e("TAG", "Нажато e"); break; default: Log.e("TAG", "Нажато что то"); } } return false; } }); 

The entire handler responds only to numbers. I press the letters or any other characters. Default does not work, I press numbers except 2 - it works. I press 2 - it works. All keys peretykat "pressed e" does not come out. And inter too. Maybe I do not know about the phone model, I have a Lenovo S650 Android 4.4.2 Api 19. There is no other phone to check.

  • And in the XML field, no filter is assigned to the input? .. - YuriSPb
  • There is no filter, EditText ʻa only id, width, height and everything - T. Kudaibergen

1 answer 1

OnKeyListener works crookedly and basically only for the numbers from the keypad, it does not perceive the other keys. Strangely, the enterter does not work either. In this case, it is better to use TextWatcher , something like this:

 myTextView.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.subSequence(count-1,count).equals("нужная кнопка")) ...выполнить нужное действие } @Override public void afterTextChanged(Editable s) { } });