Execute function with ENTER in EditText

1

I need to perform a function with the "Enter" keyboard in EditText .

They did not work:

android:maxLines="1" : it skips fields

android:imeOptions="actionNext" : it executes with the "next" button of the virtual keyboard

setOnKeyListener with keyCode == KeyEvent.KEYCODE_ENTER : blocks EditText and does not let you enter anything.

    
asked by anonymous 16.04.2018 / 19:36

1 answer

1

Running:

this.edt = findViewById(R.id.editText);

this.edt.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction() == KeyEvent.ACTION_DOWN) {

                    if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
                        metodo();
                        return true;
                    }

                }
                return false;
            }
        });

Complete list of keys: Official document

    
17.04.2018 / 12:39