How to differentiate between long key press and regular key press?

前端 未结 3 1703
庸人自扰
庸人自扰 2020-12-16 01:46

I\'m trying to override the functionality of the back key press. When the user presses it once, I want it to come back to the previous screen. However, when the back key is

3条回答
  •  我在风中等你
    2020-12-16 02:32

    Why not use onKeyUp() as well as onKeyDown()? During onKeyDown() you do not know whether it is a long press or not, because that is called as soon as the key is pressed and you do not know how long the user intends to hold the key down for. As KasperMoerch correctly says, you need to call startTracking in your onKeyDown() method, and return true. Then in your onKeyUp() you can call event.isTracking() and event.isLongPress() to determine whether to handle things as a long press or short press.

提交回复
热议问题