您的位置:首页 - 教程 - 安卓 - 正文
android中button点击频率控制
  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
    }

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

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

评论: