Android button - How to set focusable to true and still accept onClick listener on first click?

后端 未结 3 1512
暗喜
暗喜 2021-01-01 23:34

UPDATE

I\'ve solved the clicking issue by removing the two focusable lines from the button style and using the onClick event handler to call r

相关标签:
3条回答
  • 2021-01-02 00:14

    you can do like this in onClick(View v):

    .....
    v.setFocusableInTouchMode(true);
    v.requestFocus();
    v.setFocusableInTouchMode(false);
    .....//the others
    

    so, v gets focus and you can do something in onClick() through setFocusableInTouchMode(false), it can accept onClick at first touch.

    0 讨论(0)
  • 2021-01-02 00:16

    You might need to use an OnFocusChangeListener and then check if the focus event happened when the screen was in touch mode: if it did then it was a user click, otherwise it was from the trackball/trackpad.

    0 讨论(0)
  • 2021-01-02 00:17

    You can use onTouch. This way you can handle all clicks without having to set OnFocusChangeListener.

    0 讨论(0)
提交回复
热议问题