gpt4 book ai didi

java - setontouchlistener x 秒

转载 作者:行者123 更新时间:2023-12-02 08:11:13 27 4
gpt4 key购买 nike

我正在尝试使用 setontouchlistener。如果我按住图像按钮 5 秒钟,我会收到警报...这就是我要做的事情:

    final ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton1);

imageButton1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// do something
return false;
}
});

如何在 x 秒后使用 setontouchlistener 实现类似的功能?

工作解决方案:

imageButton1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {
mTouchDownTime = event.getEventTime();
Log.v("CustomDebug", "Message: " + mTouchDownTime);

}
if (event.getAction() == MotionEvent.ACTION_UP) {
long elapsedTime = event.getEventTime() - mTouchDownTime;
Log.v("CustomDebug", "Message: " + elapsedTime);

}
return false;
}
});

最佳答案

onTouch 被调用两次,第一次使用 MotionEvent ACTION_DOWN,第二次使用 MotionEvent ACTION_UP

所以伪代码:

if Event == ACTION_DOWN 
saveTime = time();
elseif Event == ACTION_UP and time()-savetime > 5
doStuff because the guy hold it for 5sec.

关于java - setontouchlistener x 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7393192/

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