带有点击事件的中断处理程序

Interrupt Handler with click event

考虑以下代码:

 ImageView v = (ImageView)findByViewId(R.id.picture);
 // do something with v
 Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            startGame();
        }
    }, 1500);

1.5 秒后 method startGame() 被执行。 如果用户想跳过这段等待时间,他应该可以点击图片立即调用startGame(),而不用在1.5秒后再次调用startGame()

为了停止您的处理程序,您需要使用以下方法。

handler.removeCallbacksAndMessages(null);

参考:RemoveCallBacksAndMessages

希望对您有所帮助。