我想以毫秒为单位测量连续点击之间的时间。我怎样才能做到这一点?
问问题
8081 次
2 回答
8
View myView;
long previousClickTime;
...
myView.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent ev)
{
long temp = System.currentTimeMillis();
if (previousClickTime != 0)
{
Log.i("MyView", "Time Between Clicks=" + (temp - previousClickTime));
}
else
{
Log.i("MyView", "First Click");
}
previousClickTime = temp;
return true;//Note that you can return false, and any underlying Views will then receive this touch event.
}
}
附带说明,如果您尝试检测双击,请尝试GuestureDetector.setOnDoubleTapListener。
于 2012-07-16T21:25:23.603 回答
1
覆盖onCLick()
事件。
活动内部onClick()
。让您的应用获得第一次点击并存储时间。然后存储第二次,得到两次抽头的差值。并且只是显示差异。
下面的代码非常简单,可能不是您要找的,但我把它放在那里作为一个非常微不足道的例子。
long initialTime = 0;
long endTime = 0;
onClick() {
if(initialTime == 0){
initialTime = System.currentTimeMillis();
} else {
endTime = System.currentTimeMillis();
long diff = endTime - initialTime;
initialTime = endTime;
Log.i("your app", "time between click: " + diff);
}
}
于 2012-07-16T20:44:26.767 回答