开发者

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:

  1. Your Activity needs to implement OnKeyListener.
  2. For your Button: mYourButton.setOnKeyListener(this);
  3. Override onKey() and handle KEYCODE_ENTER.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜