如何区分长按键和普通按键?
我正在尝试覆盖后退按键的功能。当用户按一次时,我希望它返回到上一个屏幕。但是,当长按后退键(比方说,两秒或更长时间)时,我想退出应用程序。
到目前为止,我已经在 Activity 中重写了这两个方法:
@Override
public boolean onKeyDown( int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK) {
//manage short keypress
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyLongPress( int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK) {
//manage long keypress (different code than short one)
return true;
}
return super.onKeyLongPress(keyCode, event);
}
但是 onKeyLongPress
回调永远不会被调用,因为该事件始终由 onKeyDown
方法接收。
有没有办法让这两种方法都起作用?还是必须在 onKeyDown 中全部完成并使用重复次数/毫秒来检测它?
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 long-pressed (for, let's say, two seconds or more), I want to exit the application.
By now, I have overriden these two methods in my activity:
@Override
public boolean onKeyDown( int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK) {
//manage short keypress
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyLongPress( int keyCode, KeyEvent event){
if (keyCode == KeyEvent.KEYCODE_BACK) {
//manage long keypress (different code than short one)
return true;
}
return super.onKeyLongPress(keyCode, event);
}
But the onKeyLongPress
callback is never called, because the event is always received by the onKeyDown
method.
Is there any way of having both methods working? Or has it to be done all in the onKeyDown
and use the number of repetitions/milliseconds to detect it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
onKeyLongPress
从未被调用的原因是,您在onKeyDown
中返回 true,而没有告诉框架这可能是长按 - 导致 KeyEvent 停止流经不同的事件处理程序。您需要做的是:
onKeyLongPress
中处理长按。执行如下,它将起作用:
The reason why
onKeyLongPress
is never called, is that you return true inonKeyDown
without telling the framework that this might be a long press - causing the KeyEvent to stop its flow through the different event handlers.What you need to do is this:
event.startTracking()
as explained in the documentation.onKeyLongPress
.Implement as below and it will work:
为什么不使用
onKeyUp()
以及onKeyDown()
呢?在onKeyDown()
期间,您不知道是否是长按,因为按下按键后就会调用它,并且您不知道用户打算按住按键多长时间为了。正如 KasperMoerch 正确所说,您需要在onKeyDown()
方法中调用startTracking
,并返回true
。然后在onKeyUp()
中,您可以调用event.isTracking()
和event.isLongPress()
来确定是否将事件处理为 long按或短按。Why not use
onKeyUp()
as well asonKeyDown()
? DuringonKeyDown()
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 callstartTracking
in youronKeyDown()
method, and returntrue
. Then in youronKeyUp()
you can callevent.isTracking()
andevent.isLongPress()
to determine whether to handle things as a long press or short press.我认为最好的处理方式就是这样。
我在这里看到的唯一缺点是,如果用户启用了菜单按钮,则单击菜单按钮不会发出声音。也许有一种方法可以检查此设置并使用它,或者调用默认行为。
代码:
I think the best way to handle it is as such.
The only downside I can see here, is that single clicking the menu button doesn't make a sound in case the user has it enabled. Maybe there is a way to check this setting and use it, or call the default behavior.
Code:
在我看来,当你重写 onkeypress 和 onkeylongpress 时。每次按一次或长时间按键时,它会自动调用onkeypress。为了避免这种情况,你必须在 onkeypress 中编写 event.starttracking() ,但这只会导致长按键功能,在这种情况下按键不起作用。
对于这个解决方案。您应该覆盖 onkeydown 和 onkeyup。创建一个 bool 并使用它。这是我的代码:
In my opinion when you override onkeypress and onkeylongpress. It will automatically call onkeypress everytime you press the key for once or for long. To avoid this you have to write event.starttracking() in onkeypress, but it will result only in functionality of long key press, key press would not work in this scenerio.
For this soloution. You should override onkeydown and onkeyup. make a bool and work with it. Heres my code: