码迷,mamicode.com
首页 > 移动开发 > 详细

android中button点击频率控制

时间:2016-06-19 14:13:55      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:

  public class Utils {   
      private static long lastClickTime;   
      public static boolean isFastDoubleClick() {   
          long time = System.currentTimeMillis();   
          long timeD = time - lastClickTime;   
          if ( 0 < timeD && timeD < 500) {       //500毫秒内按钮无效,这样可以控制快速点击,自己调整频率
              return true;      
          }      
          lastClickTime = time;      
          return false;      
      }   
  } 

 
                                                             //监听按钮调用方法
  public void onClick(View v) {   
      if (Utils.isFastDoubleClick()) {   
          return;   
      }   
  } 

-----------------------------------------------------------------你自己可以测试一把  绝对没问题

写一个类

package com.example.teststyle;

import Android.util.Log;


public class ButtonUtil
{
 private static long lastClickTime = 0;
 private static long DIFF = 5000;
 private static int lastButtonId = -1;
 /**
  * 判断两次点击的间隔,如果小于1000,则认为是多次无效点击
  * @return
  */
 public static boolean isFastDoubleClick()
 {
  return isFastDoubleClick(-1,DIFF);
 }
 /**
  * 判断两次点击的间隔,如果小于1000,则认为是多次无效点击
  * @return
  */
 public static boolean isFastDoubleClick(int buttonId)
 {
  return isFastDoubleClick(buttonId,DIFF);
 }

 /**
  * 判断两次点击的间隔,如果小于diff,则认为是多次无效点击
  * @param diff
  * @return
  */
 public static boolean isFastDoubleClick(int buttonId,long diff)
 {
  long time = System.currentTimeMillis();
  long timeD = time - lastClickTime;
  Log.v("xxxx", "lastButtonId = "+lastButtonId+"  buttonId = "+buttonId);
  if (lastButtonId == buttonId && lastClickTime>0 && timeD < diff)
  {
   Log.v("isFastDoubleClick", "短时间内按钮多次触发");
   return true;
  }
  
  lastClickTime = time;
  lastButtonId = buttonId;
  return false;
 }
}

 然后调用

public void onClick(View v) {
    // TODO Auto-generated method stub
    if(ButtonUtil.isFastDoubleClick(v.getId())){

      //这儿进行控制,时间自己控制
     Log.e("----->显示点击时间和开始时间", "开始时间");
     Toast.makeText(getApplicationContext(), "显示内容", Toast.LENGTH_SHORT).show();

   return
    }

   //开始写自己的业务逻辑

   ...........
   }

 

android中button点击频率控制

标签:

原文地址:http://www.cnblogs.com/zhujiabin/p/5597888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!