Delete key does not work

I added setOnKeyListener for Enter keyevent. However, adding setOnKeyListener, the delete (backspace) key does not work. When I deleted setOnKeyListener, the delete key works fine.

How to fix delete key works well?

final EditText edittext = (EditText) findViewById(R.id.editText1); edittext.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View arg0, int arg1, KeyEvent event) { // TODO Auto-generated method stub if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); in.hideSoftInputFromWindow(edittext .getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); applySearch(); } return true; } }); 
+4
source share
2 answers

If you return True , you process all the keys. Try the following:

 if (event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { // something here return true; } // otherwhise return false; 

Android: problem with overriding onKeyListener for button

+4
source

According to the documentation, onKey returns True if the listener consumes the event, otherwise false. In your case:

 @Override public boolean onKey(View arg0, int arg1, KeyEvent event) { ... return true; // Try to return false instead } 

When your method returns True , the keys are not processed, and the backspace does not work.

+3
source

Source: https://habr.com/ru/post/1415756/


All Articles