Run function with ENTER in Edittext

Asked

Viewed 790 times

1

I need to execute a function with the keyboard "Enter" on EditText.

Did not work:

android:maxLines="1": he jumps fields

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

setOnKeyListener with keyCode == KeyEvent.KEYCODE_ENTER: blocks Edittext and lets you insert nothing.

1 answer

1


Working:

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 the Keys: Official document

Browser other questions tagged

You are not signed in. Login or sign up in order to post.