赞
踩
在Android控件中,如果快速点击容易造成一些不同的bug,尤其是那种在click事件中方有耗时操作的代码,容易引起anr,并且有些性能低的机器,在用户点击多次控件的时候很容易出现问题,在车机中也会导致回弹的一系列问题(这里面包括get到的信号导致回弹),针对于这种情况需要做控件快速点击的保护方案。
判断用户是否快速点击,主要还是和一个时间阈值作对比,比如用户在500ms内快速点击某个控件,但是我们又不希望控件做出反应,核心代码如下:
- import android.os.SystemClock;
-
-
-
-
-
-
- /**
- * view fast click deal.
- * * <br>
- */
- public class FastClickUtil {
-
- private static long sLastClickTime = 0;
- private static final long MIN_DELAY_TIME = 500;
- private static long mFirstTime = 0;
- private static int mLastBtn = 0;
-
- /**
- * isFastClick.
- * @return true fast click.
- */
- public static boolean isFastClick() {
- long time = SystemClock.uptimeMillis();
- long timeD = time - sLastClickTime;
- if (0 < timeD && timeD < MIN_DELAY_TIME) {
- return true;
- } else {
- sLastClickTime = time;
- return false;
- }
- }
-
- /**
- * Is or not fast click function.
- * @param btnId view id.
- * @return true is, false is not
- */
- public static boolean isFastClick(int btnId) {
- long time = SystemClock.elapsedRealtime();
- long timeD = time - mFirstTime;
- if (mLastBtn == btnId && 0 < timeD && timeD < MIN_DELAY_TIME) {
- return true;
- } else {
- mLastBtn = btnId;
- mFirstTime = time;
- return false;
- }
- }
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。