OnTouch延迟(Android版) [英] OnTouch delay (Android)
问题描述
我有一个应用程序,我需要有在ImageButton的每个触摸后的延迟。
I have an app where I need to have a delay after each touch in an ImageButton.
我试过了Thread.sleep()方法,但我不知道这是否是对付它的最好办法。
I tried the Thread.sleep() method, but I am not sure if this is the best way to deal with it.
你们有什么建议?
任何帮助AP preciatted!
Any help is appreciatted!
一件事:我要onTouch()事件的内容被然后烧制我不想耽误X秒下一onTouch()事件。这就像到prevent用户点击次数太多的按钮。
ONE MORE THING: I want the content of the onTouch() event to be fired THEN I want to delay "X" seconds the next onTouch() event. It's like to prevent the user to click too many times in the button.
推荐答案
由于所有的触摸事件是由UI线程处理,视频下载()
将阻止你的UI线程这是(我希望)你正在寻找没有什么。
我认为,要解决你的问题的最正确的方法是使用 postDelayed在
接口它允许你开始延迟执行:的onClick
处理器(Runnable接口,长)
Since all touch events are handled by UI thread, Thread.sleep()
will block your UI thread which is (I hope) not what you are looking for.
I think the most correct way to solve your problem would be using postDelayed(Runnable, long)
interface in your onClick
handler which allows your to delay execution:
@Override
public void onClick(View v)
{
postDelayed(new Runnable()
{
@Override
public void run()
{
// do your stuff here
}
}, 10000); //10sec delay
}
更新:
如果您希望用户prevent点击太快了你的形象的看法,我强烈建议去与的onClick
,而不是 onTouch
(除非有严重的原因是什么)
If you want user to prevent clicking too fast on your image view, I strongly recommend go with onClick
rather than onTouch
(unless there are serious reasons for that)
不过,请参阅code段这可能会帮助您:
However, please see the code snippet which might help you:
private boolean blocked = false;
private Handler handler = new Handler();
@Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
if (!blocked)
{
blocked = true;
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
blocked = false;
}
}, 1000);
} else
{
return false;
}
}
return super.onTouchEvent(event);
}
这篇关于OnTouch延迟(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!