[英]I can not stop a Runnable using Handler in JAVA
我正在嘗試構建一個秒表,並且我有一個名為 start 的按鈕開始計數,在它開始后按鈕的名稱更改為停止,當我按下停止時我想暫停計數,但是當我嘗試運行newHandler.removeCallbacks(updateTimerThread);
程序繼續計數,在重置按鈕上此方法有效。
public class MainActivity extends AppCompatActivity { Button start_button, reset_button; TextView time_view; long start_time = 0, time_in_miliseconds = 0, update_time = 0, prev_time = 0; final Handler newHandler = new Handler(); Runnable updateTimerThread = new Runnable() { @Override public void run() { time_in_miliseconds = SystemClock.uptimeMillis() - start_time; update_time = time_in_miliseconds + prev_time; int sec = (int) (update_time / 1000); int min = sec / 60; int hour = (int) sec / 3600; sec %= 60; int centisec = (int) (update_time / 10) % 100; time_view.setText(""+hour+":"+String.format("%2d",min)+":"+String.format("%2d",sec)+"."+String.format("%3d",centisec)); newHandler.postDelayed(this, 10); if (start_button.isPressed()) { prev_time += time_in_miliseconds; // Here the Runnable is not stopped newHandler.removeCallbacks(updateTimerThread); //time_view.setText(""+save_hour+":"+String.format("%2d",save_min)+":"+String.format("%2d",save_sec)+"."+String.format("%3d",save_centisec)); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start_button = (Button) findViewById(R.id.start_button); reset_button = (Button) findViewById(R.id.reset_button); time_view = (TextView) findViewById(R.id.time_view); start_button.setOnClickListener(view -> { start_time = SystemClock.uptimeMillis(); start_button.setText("Stop"); newHandler.postDelayed(updateTimerThread,0); }); if (start_button.isPressed()) { prev_time += time_in_miliseconds; newHandler.removeCallbacks(updateTimerThread); //time_view.setText(""+save_hour+":"+String.format("%2d",save_min)+":"+String.format("%2d",save_sec)+"."+String.format("%3d",save_centisec)); } reset_button.setOnClickListener(view -> { // Here the Runnable is stopped newHandler.removeCallbacks(updateTimerThread); start_time = 0; prev_time = 0; time_view.setText("0:0:0.0"); start_button.setText("Start"); }); } }
請刪除您正在檢查start_button.isPressed()
的所有條件塊,它不起作用。 您可以使用附加標志isRunning
來指示時鍾是否正在運行:
private boolean isRunning;
start_button.setOnClickListener(view -> {
if (isRunning) {
newHandler.removeCallbacks(updateTimerThread);
start_button.setText("Start");
} else {
start_time = SystemClock.uptimeMillis();
start_button.setText("Stop");
newHandler.post(updateTimerThread);
}
isRunning = !isRunning;
});
您的計時器需要一些 state。 你可以做這樣的事情(添加'boolean isActive'):
Runnable updateTimerThread = new Runnable() {
boolean isActive = true;
@Override
public void run() {
if (isActive) {
time_in_miliseconds = SystemClock.uptimeMillis() - start_time;
update_time = time_in_miliseconds + prev_time;
// ...
}
if (start_button.isPressed()) {
isActive = !isActive;
// Do some other stuff
}
}
};
或者可以使用 State 模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.