0

我正在尝试实现长按键识别,我已经覆盖了 onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
//          event.startTracking();
        if (backPressedStarted == -1) {
            progressBar.setProgress(0);
            progressBar.setVisibility(View.VISIBLE);
            backPressedStarted = System.currentTimeMillis();        
        } else {
            int pressDuration = (int) (System.currentTimeMillis() - backPressedStarted);
            progressBar.setProgress(pressDuration);
            if (pressDuration > 5000)
                showExitDialog();
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

大约 3.8 秒后,系统调度一个带有标志的 onKeyUp 事件:FLAG_CANCELED

我试过忽略它并在 key up 方法中返回 true 或 false,但它似乎不起作用......

有没有解决的办法?我可以定义最长的新闻持续时间吗?

== 更新 ==

好的,所以当我删除事件跟踪的评论时,会调用长点击回调,然后返回true或false仍然不会改变keyUp取消的最终结果。

谢谢。

4

1 回答 1

0

After digging and trying multiple approaches attempting to override this, (with the exception of injecting my own custom touch event, i didn't feel like managing this hack), I've found out that this is not possible to avoid this "feature".

My solution was to shortened the time from 5000ms, to 3000ms, and allowed another exit pattern a side of this one, for devices with OS that might surprise me with a cancel event before 3000ms...

于 2013-08-09T20:09:34.670 回答