我有一个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;
}
});
答案 0 :(得分:0)
您可以使用
handler.removeCallbacksAndMessages(null);
一次删除所有runnable。