Android execute function after pressing "Enter" for EditText Android execute function after pressing "Enter" for EditText android android

Android execute function after pressing "Enter" for EditText


To receive a keyboard event, a View must have focus. To force this use:

edittext.setFocusableInTouchMode(true);edittext.requestFocus();

After that continue with the same code in the example:

edittext.setOnKeyListener(new View.OnKeyListener() {    public boolean onKey(View v, int keyCode, KeyEvent event) {        // If the event is a key-down event on the "enter" button        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&            (keyCode == KeyEvent.KEYCODE_ENTER)) {          // Perform action on key press          Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();          return true;        }        return false;    }});


From what I can see, It looks like you have the wrong import.

Try

edittext.setOnKeyListener(new View.OnKeyListener() {

OR add this import

import android.view.View.OnKeyListener;

and Remove this one

import android.content.DialogInterface.OnKeyListener;


Delete the import statement that has DialogInterface, then import the View.OnKeyListener.