Android按钮释放停止Runnable处理程序

时间:2018-03-29 10:31:39

标签: android handler runnable ontouchlistener

我有一个button在按下时开始运行Runnable Handler,它应stop并在释放时将其返回到起始位置。问题是,在else if条款中,它无法识别runnable,因此我无法在其上调用handler.removeCallbacks()

 playRecordButton.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionevent) {
            final int action = motionevent.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                Log.i("repeatBtn", "MotionEvent.ACTION_DOWN");
                playAudio(index);
                final Runnable runnable = new Runnable() {
                    public void run() {

                        assa.setMinX(orderNr);
                        assa.setMaxX(orderNr+5);

                        graph.invalidate();

                        handler.postDelayed(this, 80);
                    }
                };

                handler.postDelayed(runnable, 80);
            } else if (action == MotionEvent.ACTION_UP) {
                Log.i("repeatBtn", "MotionEvent.ACTION_UP");
                assa.setMinX(0);
                assa.setMaxX(2);
                handler.removeCallbacks(runnable);  
                //Here it doesnt know what runnable is and I can't make it global.

            }
            return false;
        }
    });

1 个答案:

答案 0 :(得分:0)

您可以使用

 handler.removeCallbacksAndMessages(null);  

一次删除所有runnable。