Android停止可运行的处理程序按钮释放



i有一个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;
        }
    });

您可以使用

 handler.removeCallbacksAndMessages(null);  

一次删除所有可运行的人。

最新更新