Is pressing Enter key on the keyboard not equivalent to a mouse click in Android?
The buttons in my layout are not getting pressed on Enter key. When I click on them using mouse, the code inside onClic开发者_C百科k() is getting executed. But when I press the Enter key, onClick() is not getting called.
I know that for keys, I must be using onKeyListener instead. However, does onClick() not work for both mouse click and keyboard 'Enter'? Also, the up and down navigation using arrow keys are working just fine, it is only "Enter" key that is not working!
In my app, I am using nested linear layouts, a theme(button styling), a selector and an anonymous implementation of OnFocusChangeListener. Also, I am using an emulator with specified aspect ratio. Could any of this be attributed to this issue?
Any help would be much appreciated!
- Kiki
I faced the same problem. Use Image Button instead of Button.
Furthermore, If you still want to use Button then:
- Your Activity needs to implement OnKeyListener.
- For your Button: mYourButton.setOnKeyListener(this);
- Override onKey() and handle KEYCODE_ENTER.
Hope this helps.
精彩评论