gpt4 book ai didi

android - 检测长按 "virtual"后退键

转载 作者:太空狗 更新时间:2023-10-29 16:23:31 26 4
gpt4 key购买 nike

还没有找到解决办法。我想检测对“虚拟”后退键的长按,例如您在 galaxy s2 上找到的那个。

我尝试检测用户按下按钮时的 onKeyDown 事件(有效)和用户离开按钮时的 onKeyUp 事件,但虚拟键一直触发 onKeyUp 事件。

我也尝试覆盖 onKeyLongPress 方法,但这没有帮助。

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

@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
mBackKeyUpTime = event.getEventTime();
final long delta = mBackKeyUpTime - mBackKeyDownTime;
return true;
}
return super.onKeyUp(keyCode, event);
}

最佳答案

试试这个...

    int LONG_PRESS_TIME = 5000; // Time in miliseconds
Handler handler;
Runnable longPressed;

handler = new Handler();
longPressed = new Runnable() {
public void run() {
Log.d("LogTag", "Long Pressed");
}
};

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
handler.postDelayed(longPressed, LONG_PRESS_TIME);
return true;
}
return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
handler.removeCallbacks(longPressed);
return true;
}
return super.onKeyUp(keyCode, event);
}

关于android - 检测长按 "virtual"后退键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8378012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com