在Android中给我们提供了单次点击事件。但并没有给我们提供双击,或者实现在一定时间内的多次事件。所以需要我们自己在单机监听上进行修改实现。
有如下两种实现方式:
1、定义一个存贮上一个第一次点击的变量,如果两次时间间隔小于500毫秒,则认为是双击时间。
实现如下:
package com.andy.doubleclick;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Toast;
/**
* @author Zhang,Tianyou
* @version 2014年12月02日 上午10:51:56
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private long startClickTime;
public void click(View view) {
long nextClickTime = SystemClock.uptimeMillis();
if (startClickTime <= 0) {
startClickTime = SystemClock.uptimeMillis();
return ;
}else {
if (nextClickTime - startClickTime < 500) {
Toast.makeText(this, "被双击了", Toast.LENGTH_SHORT).show();
startClickTime = 0L;
} else {
startClickTime = SystemClock.uptimeMillis();
}
}
}
}
2、使用Google提供的api中采用的算法。
能够实现n次点击事件,我们需要定义一个n长度的数组,每点击一次将数组里的内容按序号整体向左移动一格,然后给n-1出即数组的最后添加当前的时间,如果0个位置的时间大于当前时间减去500毫秒的话,那么证明在500毫秒内点击了n次。
实现如下:
package com.andy.doubleclick;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Toast;
/**
* @author Zhang,Tianyou
* @version 2014年12月02日 上午10:51:56
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
long[] mHits = new long[2];
public void click(View view){
//每点击一次 实现左移一格数据
System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
//给数组的最后赋当前时钟值
mHits[mHits.length - 1] = SystemClock.uptimeMillis();
//当0出的值大于当前时间-500时 证明在500秒内点击了2次
if(mHits[0] > SystemClock.uptimeMillis() - 500){
Toast.makeText(this, "被双击了", Toast.LENGTH_SHORT).show();
}
}
}
System.currentTimeMillis() 和 SystemClock.uptimeMillis()的区别:
在Api上是这么说的:
System.currentTimeMillis()
is the standard "wall" clock (time and date) expressing milliseconds since the epoch. The wall clock can be set by the user or the phone network (see setCurrentTimeMillis
), so the time may jump backwards or forwards unpredictably. This clock should only be used when correspondence with real-world dates and times is important, such as in a calendar or alarm clock application. Interval or elapsed time measurements should use a different clock. If you are using System.currentTimeMillis(), consider listening to the ACTION_TIME_TICK
, ACTION_TIME_CHANGED
and ACTION_TIMEZONE_CHANGED
Intent
broadcasts to find out when the time changes.
SystemClock.uptimeMillis
is counted in milliseconds since the system was booted. This clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input), but is not affected by clock scaling, idle, or other power saving mechanisms. This is the basis for most interval timing such as Thread.sleep(millls)
, Object.wait(millis)
, and System.nanoTime()
. This clock is guaranteed to be monotonic, and is suitable for interval timing when the interval does not span device sleep. Most methods that accept a timestamp value currently expect the uptimeMillis
clock.
SystemClock.elapsedRealtime
and elapsedRealtimeNanos
return the time since the system was booted, and include deep sleep. This clock is guaranteed to be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend basis for general purpose interval timing.
SystemClock.elapsedRealtime
: 从开机到现在的毫秒书(手机睡眠(sleep)的时间也包括在内)
System.currentTimeMillis()
:从1970年1月1日 UTC到现在的毫秒数,是可以通过System.setCurrentTimeMillis修改的,那么,在某些情况下,一但被修改,时间间隔就不准了。
SystemClock.uptimeMillis
: 它表示的是手机从启动到现在的运行时间,且不包括系统sleep(CPU关闭)的时间,很多系统的内部时间都是基于此,比如Thread.sleep(millls)
, Object.wait(millis)
, and System.nanoTime()
它表示的是手机从启动到现在的运行时间,且不包括系统sleep(CPU关闭)的时间,很多系统的内部时间都是基于此,比如Thread.sleep(millls)
, Object.wait(millis)
, and System.nanoTime()