试图赶上卷onKeyLong preSS()不工作 [英] Trying to catch the Volume onKeyLongPress() not working

查看:189
本文介绍了试图赶上卷onKeyLong preSS()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的应用程序有以下code反应很长的关键preSS上降低音量:

I'm trying to get my app to react to a long key press on volume down with the following code:

public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        Log.w("myApp", "LONG PRESS");
    }
    return super.onKeyLongPress(keyCode, event);
}

然而,这只是修建垃圾一堆 onKey preSS的()音量事件下来, onKeyLong preSS() 永远不会被调用。我的目的是要离开音量向下和向上短presses独自一人,并有我的应用程序有不同的反应体积长preSS。
任何人都可以指出我错过了什么?

However, it only spams a bunch of onKeyPress() events for volume down, and onKeyLongPress() never gets called. My intention is to leave the volume down and up "short" presses alone, and have my app react differently to the volume long press.
Can anybody point out what I'm missing?

推荐答案

您几乎没有。需要检测在onKey preSS的处理程序相同的键事件,并开始跟踪它使长preSS可以工作。在这里,您需要的code:

You are almost there. You need to detect the same key event in the onKeyPress handler and start tracking it so that the long press can work. Here's the code you need:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        event.startTracking();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

这篇关于试图赶上卷onKeyLong preSS()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆