我想在点击5次时发出意图。问题是,当我点击3次并暂停一段时间再次点击2次时,意图被解雇...
我只想在连续点击5次时才想要它。
static int i = 0;
public void onClick(View view) {
i++;
if (i == 5) {
i = 0;
Intent myIntent = new Intent(activity, loginActivity.class);
activity.startActivity(myIntent);
}
}
是否可以配置点击之间的最大延迟?
答案 0 :(得分:1)
您可以存储最后一次点击的时间戳,并在自上次点击后的延迟超过阈值时重置点击计数器:
private static final double MAX_CLICK_DELAY = 500; // 500ms
private double lastClickTime = 0;
private int i = 0;
public void onClick(View view) {
double currentTime = System.currentTimeMillis();
if(currentTime - lastClickTime > MAX_CLICK_DELAY) {
i = 0;
}
lastClickTime = currentTime;
i++;
if (i == 5) {
i = 0;
Intent myIntent = new Intent(activity, loginActivity.class);
activity.startActivity(myIntent);
}
}