Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android編程入門 >> android中button點擊頻率控制

android中button點擊頻率控制

編輯:Android編程入門

  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
    }

   //開始寫自己的業務邏輯

   ...........
   }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved