使用Handler的removeCallbacks()方法来清除延迟的消息,避免错乱。以下是解决方案的代码示例:
Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { //do something } }; handler.postDelayed(runnable, 2000);
//使用removeCallbacks()方法清除延迟消息 handler.removeCallbacks(runnable);