Android的触摸事件确定时间 [英] Android Touch Event determining duration
问题描述
一个人如何检测的Android 2.1触摸事件的持续时间?我想回应只有在地区一直pssed的发言权5秒$ P $?
How does one detect the duration of an Android 2.1 touch event? I would like to respond only if the region has been pressed for say 5 seconds?
推荐答案
您可以尝试混合<一href="http://developer.android.com/reference/android/view/MotionEvent.html"><$c$c>MotionEvent$c$c>和 的Runnable
/的 处理程序
以实现这一目标。
You could try mixing MotionEvent
and Runnable
/Handler
to achieve this.
样品code:
private final Handler handler = new Handler();
private final Runnable runnable = new Runnable() {
public void run() {
checkGlobalVariable();
}
};
// Other init stuff etc...
@Override
public void onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Execute your Runnable after 5000 milliseconds = 5 seconds.
handler.postDelayed(runnable, 5000);
mBooleanIsPressed = true;
}
if(event.getAction() == MotionEvent.ACTION_UP) {
if(mBooleanIsPressed) {
mBooleanIsPressed = false;
handler.removeCallbacks(runnable);
}
}
}
现在你只需要检查 mBooleanIs pressed
是真
在 checkGlobalVariable()
的功能。
Now you only need to check if mBooleanIsPressed
is true
in the checkGlobalVariable()
function.
我来到了,当我在写这是使用简单的时间戳有一个想法(如 System.currentTimeMillis的()
)以确定的 MotionEvent.ACTION_DOWN
和 MotionEvent.ACTION_UP
。
One idea I came up with when I was writing this was to use simple timestamps (e.g. System.currentTimeMillis()
) to determine the duration between MotionEvent.ACTION_DOWN
and MotionEvent.ACTION_UP
.
这篇关于Android的触摸事件确定时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!