Skip to content Skip to sidebar Skip to footer

Android: Problem With Overriding Onkeylistener For A Button

I want a certain functionality when Enter key is pressed on a Button. When I override onKey(), I write the code to be executed for KEY_ENTER. This works fine. setupButton.setOnKey

Solution 1:

When you return true in the function, that tells Android you are handling all keys, not just the Enter key.

You should return true only at the end, inside the if statement and return false at the end of the function.

setupButton.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (KeyEvent.KEYCODE_ENTER == keyCode)
            {
                Intent setupIntent = new Intent(getApplicationContext(),SetUp.class);
                startActivityForResult(setupIntent, RESULT_OK);
                returntrue;
            }
            returnfalse;
        }
    });

Post a Comment for "Android: Problem With Overriding Onkeylistener For A Button"