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));
            }
        });



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。