android 和 java 中的双击问题
Double Click Problem in android with java
我已经创建了一个 Java Class 用于双击,但我无法双击该项目,因为当我只单击一次时,该项目不会等待双击!
双击Class代码
package com.programiner.studyvillaadmin.Classes.Listener;
import android.view.View;
public abstract class DoubleClickListener implements View.OnClickListener {
private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds
long lastClickTime = 0;
@Override
public void onClick(View v) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
onDoubleClick(v);
lastClickTime = 0;
} else {
onSingleClick(v);
}
lastClickTime = clickTime;
}
public abstract void onSingleClick(View v);
public abstract void onDoubleClick(View v);
}
private long mLastClickTime = 0;
@Override
public void onClick(View v) {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) //*1000 is time to perform second click*
return; *// return when second click happen in second*
mLastClickTime = SystemClock.elapsedRealtime();
GameSound.getInstance(getApplicationContext()).sound__(GameSound.buttonClick);
if (v == findViewById(R.id.tvUsername)) {
StartActivity(UserProfile.class);
}
}
例子
public long mLastClickTime = 0;
//声明和定义public
holder.itemView.setOnClickListener(new View.OnClickListener() { //click lister of itemview of RV Adapter
@Override
public void onClick(View v) {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) //*1000 is time to perform second click*
return; // return when second click happen in second*
mLastClickTime = SystemClock.elapsedRealtime();
//do stuff here...
}
});
我已经创建了一个 Java Class 用于双击,但我无法双击该项目,因为当我只单击一次时,该项目不会等待双击!
双击Class代码
package com.programiner.studyvillaadmin.Classes.Listener;
import android.view.View;
public abstract class DoubleClickListener implements View.OnClickListener {
private static final long DOUBLE_CLICK_TIME_DELTA = 300;//milliseconds
long lastClickTime = 0;
@Override
public void onClick(View v) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
onDoubleClick(v);
lastClickTime = 0;
} else {
onSingleClick(v);
}
lastClickTime = clickTime;
}
public abstract void onSingleClick(View v);
public abstract void onDoubleClick(View v);
}
private long mLastClickTime = 0;
@Override
public void onClick(View v) {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) //*1000 is time to perform second click*
return; *// return when second click happen in second*
mLastClickTime = SystemClock.elapsedRealtime();
GameSound.getInstance(getApplicationContext()).sound__(GameSound.buttonClick);
if (v == findViewById(R.id.tvUsername)) {
StartActivity(UserProfile.class);
}
}
例子
public long mLastClickTime = 0;
//声明和定义public
holder.itemView.setOnClickListener(new View.OnClickListener() { //click lister of itemview of RV Adapter
@Override
public void onClick(View v) {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) //*1000 is time to perform second click*
return; // return when second click happen in second*
mLastClickTime = SystemClock.elapsedRealtime();
//do stuff here...
}
});