Android:長按連續響應的Button

在電商類app的購物車頁面,經常會有這樣的需求:長按“+”按鈕或者“-”按鈕時,購物車中的商品數量連續的增加或減少。

本例的目的就是爲了實現這個效果。預覽圖:


1.自定義Button.

/**
 * 長按連續響應的Button
 * Created by admin on 15-6-1.
 */
public class LongClickButton extends Button {
    /**
     * 長按連續響應的監聽,長按時將會多次調用該接口中的方法直到長按結束
     */
    private LongClickRepeatListener repeatListener;
    /**
     * 間隔時間(ms)
     */
    private long intervalTime;

    private MyHandler handler;

    public LongClickButton(Context context) {
        super(context);

        init();
    }

    public LongClickButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    public LongClickButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    /**
     * 初始化監聽
     */
    private void init() {
        handler = new MyHandler(this);
        setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new Thread(new LongClickThread()).start();
                return true;
            }
        });
    }

    /**
     * 長按時,該線程將會啓動
     */
    private class LongClickThread implements Runnable {
        private int num;

        @Override
        public void run() {
            while (LongClickButton.this.isPressed()) {
                num++;
                if (num % 5 == 0) {
                    handler.sendEmptyMessage(1);
                }

                SystemClock.sleep(intervalTime / 5);
            }
        }
    }

    /**
     * 通過handler,使監聽的事件響應在主線程中進行
     */
    private static class MyHandler extends Handler {
        private WeakReference<LongClickButton> ref;

        MyHandler(LongClickButton button) {
            ref = new WeakReference<>(button);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            LongClickButton button = ref.get();
            if (button != null && button.repeatListener != null) {
                button.repeatListener.repeatAction();
            }
        }
    }

    /**
     * 設置長按連續響應的監聽和間隔時間,長按時將會多次調用該接口中的方法直到長按結束
     *
     * @param listener     監聽
     * @param intervalTime 間隔時間(ms)
     */
    public void setLongClickRepeatListener(LongClickRepeatListener listener, long intervalTime) {
        this.repeatListener = listener;
        this.intervalTime = intervalTime;
    }

    /**
     * 設置長按連續響應的監聽(使用默認間隔時間100ms),長按時將會多次調用該接口中的方法直到長按結束
     *
     * @param listener 監聽
     */
    public void setLongClickRepeatListener(LongClickRepeatListener listener) {
        setLongClickRepeatListener(listener, 100);
    }

    public interface LongClickRepeatListener {
        void repeatAction();
    }
}
2.在Activity中調用:

LongClickButton buttonSub = (LongClickButton) findViewById(R.id.long_click_button1);
        LongClickButton buttonAdd = (LongClickButton) findViewById(R.id.long_click_button2);
        final TextView numberTV = (TextView) findViewById(R.id.main_number);

        //連續減
        buttonSub.setLongClickRepeatListener(new LongClickButton.LongClickRepeatListener() {
            @Override
            public void repeatAction() {
                numberTV.setText(String.valueOf(Integer.parseInt(numberTV.getText().toString()) - 1));
            }
        }, 50);
        //連續加
        buttonAdd.setLongClickRepeatListener(new LongClickButton.LongClickRepeatListener() {
            @Override
            public void repeatAction() {
                numberTV.setText(String.valueOf(Integer.parseInt(numberTV.getText().toString()) + 1));
            }
        }, 50);

        //減1
        buttonSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numberTV.setText(String.valueOf(Integer.parseInt(numberTV.getText().toString()) - 1));
            }
        });
        //加1
        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numberTV.setText(String.valueOf(Integer.parseInt(numberTV.getText().toString()) + 1));
            }
        });

3.很多時候,長按的事件和普通點擊的事件是一樣的(也就是說,長按相當於連續的快速點擊)。這種情況下,自定義Button可以更加簡潔:即在長按時,連續的調用普通的OnClickListener。

public class LongClickButton2 extends Button {
    /**
     * 間隔時間(ms)
     */
    private long intervalTime = 50;

    private MyHandler handler;

    public LongClickButton2(Context context) {
        super(context);

        init();
    }

    public LongClickButton2(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    public LongClickButton2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    /**
     * 初始化監聽
     */
    private void init() {
        handler = new MyHandler(this);
        setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new Thread(new LongClickThread()).start();
                return true;
            }
        });
    }

    /**
     * 長按時,該線程將會啓動
     */
    private class LongClickThread implements Runnable {
        private int num;

        @Override
        public void run() {
            while (LongClickButton2.this.isPressed()) {
                num++;
                if (num % 5 == 0) {
                    handler.sendEmptyMessage(1);
                }

                SystemClock.sleep(intervalTime / 5);
            }
        }
    }

    /**
     * 通過handler,使監聽的事件響應在主線程中進行
     */
    private static class MyHandler extends Handler {
        private WeakReference<LongClickButton2> ref;

        MyHandler(LongClickButton2 button) {
            ref = new WeakReference<>(button);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            LongClickButton2 button = ref.get();
            if (button != null) {
                //直接調用普通點擊事件
                button.performClick();
            }
        }
    }

    public void setIntervalTime(long intervalTime) {
        this.intervalTime = intervalTime;
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章