I have been following the official Android tutorials and somehow am having a problem with this very simple example to execute a function after pressing \"Enter\" for an Edit
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
.
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;
}
});