user10095135
user10095135

Reputation:

When enter is clicked

I have a edittext that brings up the numpad. I want when the user clicks the 'enter' button to do something.

I have tried this but it doesn't seem to work:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_NUMPAD_ENTER:
            check_the_code();
            return true;
        default:
            return super.onKeyUp(keyCode, event);
    }
}

P.S. Is there a way to disable 'enter' from closing the numpad?

Upvotes: 0

Views: 195

Answers (2)

Mbuodile Obiosio
Mbuodile Obiosio

Reputation: 1543

EditText editText = (EditText) findViewById(R.id.edittext);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
            handled = true;
        }
        return handled;
    }
});

Upvotes: 1

Nigel Brown
Nigel Brown

Reputation: 450

Try looking for this to be pressed instead:

KeyEvent.KEYCODE_ENTER 

try logging what the value of enter is returning by checking the int value of the key (KeyCode) then compare it to the key values from KeyEvent you are looking to use to see if it is what you expect.

you should get the value 160 for KEYCODE_NUMPAD_ENTER and 66 for KEYCODE_ENTER.

Upvotes: 1

Related Questions