android 双击事件的实现

//firstClickTime为第一次点击时的系统记录时间

if (firstClickTime > 0) {

long secondClickTime = SystemClock.uptimeMillis();  //secondClickTime 为第二次点击时的系统记录时间
long dTime = secondClickTime - firstClickTime;//两次点击的时间间隔
if (dTime < 500) {
Toast.makeText(this, "双击", 0).show();
} else {
firstClickTime = 0;
}
return;
}
firstClickTime = SystemClock.uptimeMillis(); //线程延迟减小误差
new Thread(){
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
firstClickTime=0;
};

}.start();


//实现多次点击

首先定义一个数据,数组的长度为点击的次数,以下代码已三次点击为例

long time=new long[3];

//点击事件

public void click(View view) {

System.arraycopy(time, 1, time, 0, time.length-1); //数据拷贝,左移,最后一位存储当前点击时的系统时间
time[time.length-1]=SystemClock.uptimeMillis();
if(time[0]>=(SystemClock.uptimeMillis()-500)){
Toast.makeText(MainActivity.this, "三次点击", 0).show();

}
}

你可能感兴趣的:(android)