android中button点击频率控制
Posted 星辰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 } //开始写自己的业务逻辑 ........... }
以上是关于android中button点击频率控制的主要内容,如果未能解决你的问题,请参考以下文章
android button控制linearlayout的显示隐藏
片段中的 button.setOnClickListener 不起作用