1拯啦、彈窗
點(diǎn)擊完按鈕彈出一個(gè)彈窗,等后續(xù)的事件執(zhí)行完成之后再關(guān)閉彈窗熔任,但是這種做法用戶體驗(yàn)較差褒链,并且適用的場(chǎng)景比較單一,只能在網(wǎng)絡(luò)請(qǐng)求或者其他耗時(shí)操作的時(shí)候使用笋敞。
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setIndeterminate(false);
progressDialog.show();
2、禁用按鈕
點(diǎn)擊完按鈕之后禁用該按鈕荠瘪,等后續(xù)事件執(zhí)行完之后再啟用按鈕夯巷。跟彈出彈窗一個(gè)邏輯,也是適用場(chǎng)景比較單一哀墓。
mBtn.setEnabled(false);
3趁餐、根據(jù)點(diǎn)擊間隔時(shí)間判斷
這種方法比較常用,任何點(diǎn)擊事件都可以使用篮绰,缺點(diǎn)是每個(gè)點(diǎn)擊事件都要進(jìn)行判斷后雷,冗余代碼較多。
public class FastClickUtil {
/**
* 上次點(diǎn)擊的view的Id
*/
private static int mLastViewId = -1;
/**
* 上次點(diǎn)擊的時(shí)間
*/
private static long mLastClickTime = 0L;
/**
* 兩次點(diǎn)擊時(shí)間間隔
*/
private static final long FAST_CLICK_DELAY_DURATION = 500L;
public static boolean isFastClick() {
return isFastClick(-1, FAST_CLICK_DELAY_DURATION);
}
public static boolean isFastClick(int viewId) {
return isFastClick(viewId, FAST_CLICK_DELAY_DURATION);
}
public static boolean isFastClick(int viewId, long delayDuration) {
long currentTime = System.currentTimeMillis();
long diffTime = currentTime - mLastClickTime;
if (viewId == mLastViewId && mLastClickTime > 0 && diffTime < delayDuration) {
return true;
}
mLastViewId = viewId;
mLastClickTime = currentTime;
return false;
}
}
4、AOP
使用Java切面臀突,用注解的形式來(lái)實(shí)現(xiàn)勉抓。好處是使用的時(shí)候很簡(jiǎn)潔,一個(gè)注解即可候学。缺點(diǎn)是需要引入相關(guān)的庫(kù)和插件藕筋,并且可能因?yàn)榛煜鹨恍﹩?wèn)題。
引入依賴:
在項(xiàng)目根目錄下的build.gradle中引入gradle-android-plugin-aspectjx:
buildscript {
...
dependencies {
...
classpath "com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10"
}
}
在module下的build.gradle中引入依賴:
dependencies {
...
implementation 'org.aspectj:aspectjrt:1.9.5'
}
在module下的build.gradle中應(yīng)用插件:
plugins {
...
id 'android-aspectjx'
}
同步項(xiàng)目梳码。
添加注解:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SingleClick {
//間隔時(shí)間
long value() default 500;
}
工具類:
public class FastClickUtil {
private static long mLastClickTime;
private static int mLastClickViewId;
public static boolean isFastClick(View view, long intervalMillis) {
int viewId = view.getId();
long time = System.currentTimeMillis();
long timeInterval = Math.abs(time - mLastClickTime);
if (viewId == mLastClickViewId && timeInterval < intervalMillis) {
return true;
}
mLastClickTime = time;
mLastClickViewId = viewId;
return false;
}
}
實(shí)現(xiàn)AOP操作:
@Aspect
public class SingleClickAspect {
@Pointcut("execution(@site.exciter.lib.fastclick.SingleClick * *(..))")
public void methodAnnotated() {
}
@Around("methodAnnotated()")
public void aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
View view = null;
for (Object arg : joinPoint.getArgs()) {
if (arg instanceof View) {
view = (View) arg;
break;
}
}
if (view == null) {
return;
}
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
if (!method.isAnnotationPresent(SingleClick.class)) {
return;
}
SingleClick singleClick = method.getAnnotation(SingleClick.class);
if (singleClick != null && !FastClickUtil.isFastClick(view, singleClick.value())) {
joinPoint.proceed();
}
}
}
使用:
@SingleClick(1000)
public void onClick(View view) {
...
}
5隐圾、通過(guò)Listener攔截
實(shí)現(xiàn)View.OnClickListener
,根據(jù)時(shí)間差去攔截點(diǎn)擊事件掰茶。
public abstract class ClickProtector implements View.OnClickListener {
/**
* 點(diǎn)擊間隔
*/
private long mDelay = 0;
/**
* 實(shí)際點(diǎn)擊事件
*
* @param view View
*/
abstract void onRealClick(View view);
public ClickProtector delay(long delay) {
this.mDelay = delay;
return this;
}
@Override
public void onClick(View view) {
int key = view.hashCode();
Long lastTime = (Long) view.getTag(key);
//已經(jīng)點(diǎn)擊過(guò)并且時(shí)間差小于設(shè)定的點(diǎn)擊間隔暇藏,就攔截
if (lastTime != null && System.currentTimeMillis() - lastTime < mDelay) {
return;
}
//觸發(fā)點(diǎn)擊事件
onRealClick(view);
//記錄本次點(diǎn)擊的時(shí)間
view.setTag(key, System.currentTimeMillis());
}
}
使用:
findViewById(R.id.btn_click).setOnClickListener(new ClickProtector() {
@Override
void onRealClick(View view) {
Toast.makeText(RepeatClickActivity.this, "點(diǎn)擊了按鈕", Toast.LENGTH_SHORT).show();
}
}.delay(500));
關(guān)注木水小站 (zhangmushui.cn)和微信公眾號(hào)【木水Code】,及時(shí)獲取更多最新技術(shù)干貨濒蒋。