本文主要是介绍android防止按键过快的点击,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第一种
正常的view.onClickListener实现onClick接口 :
自定义一个NoDoubleClickListener,继承自OnClickListener:
public abstract class NoDoubleClickListener implements OnClickListener {public static final int MIN_CLICK_DELAY_TIME = 1000;//这里设置不能超过多长时间private long lastClickTime = 0;protected abstract void onNoDoubleClick(View v);@Overridepublic void onClick(View v) {long currentTime = Calendar.getInstance().getTimeInMillis();if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {lastClickTime = currentTime;onNoDoubleClick(v);} }
给view设置点击事件时用NoDoubleClickListener代替OnClickListener,并且实现方法onNoDoubleClick代替onClick即可,像这样:
view.setOnClickListener(new NoDoubleClickListener() {@Overridepublic void onNoDoubleClick(View v) {}});
第二种 使用插件或者ButterKnife 生成的onClick事件
第一个类类似于NoDoubleClickListener 判断下两次点击的时间
public class OneClick {private String methodName;private static final int CLICK_DELAY_TIME = 1000;private long lastClickTime = 0;public OneClick(String methodName) {this.methodName = methodName;}public String getMethodName() {return methodName;}public boolean check() {long currentTime = Calendar.getInstance().getTimeInMillis();if (currentTime - lastClickTime > CLICK_DELAY_TIME) {lastClickTime = currentTime;return false;} else {return true;}}
}
第二个类静态变量存储点击事件,可以定制存储的数量超出了可以自动删除之前的。
public class AntiShake {private static LimitQueue<OneClick> queue = new LimitQueue<>(20);public static boolean check(Object o) {String flag;if(o == null) {flag = Thread.currentThread().getStackTrace()[2].getMethodName();} else {flag = o.toString();}for (OneClick util : queue.getArrayList()) {if (util.getMethodName().equals(flag)) {return util.check();}}OneClick clickUtil = new OneClick(flag);queue.offer(clickUtil);return clickUtil.check();}
}
第三个类就是存储一定对象的链表,超出了定义的范围就删除第一个对象
public class LimitQueue<E> {private int limitedSize;private LinkedList<E> linkedList = new LinkedList<>();public LimitQueue(int size) {this.limitedSize = size;}public void offer(E e) {if (linkedList.size() >= limitedSize) {linkedList.poll();}linkedList.offer(e);}public E get(int position) {return linkedList.get(position);}public E getLast() {return linkedList.getLast();}public E getFirst() {return linkedList.getFirst();}public int getLimit() {return limitedSize;}public void setLimitedSize(int size) {this.limitedSize = size;}public int size() {return linkedList.size();}public ArrayList<E> getArrayList() {ArrayList<E> arrayList = new ArrayList<>();for (int i = 0; i < linkedList.size(); i ++) {arrayList.add(linkedList.get(i));}return arrayList;}@Overridepublic String toString() {StringBuilder buffer = new StringBuilder();for (int i = 0; i < linkedList.size(); i++) {buffer.append(linkedList.get(i));buffer.append(" ");}return buffer.toString();}
}
@OnClick({R.id.record, R.id.combine})
public void onClick(View view) {if (AntiShake.check(view.getId())) { //判断是否多次点击return; }switch (view.getId()) {case R.id.hos_detail_visit_record://todobreak;case R.id.combine_chart://todo}
}
最后在onClick中使用AntiShake.check(view.getId())判断
这篇关于android防止按键过快的点击的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!