1. 程式人生 > >android防止按鍵過快的點選

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);
		@Override
		public 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() {
            @Override
            public 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;
    }

    @Override
    public 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:
           //todo
            break;
        case R.id.combine_chart:
            //todo
    }
}

最後在onClick中使用AntiShake.check(view.getId())判斷