我无法在 JAVA 中使用 Handler 停止 Runnable
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() {
private boolean isActive;
@Override
public void run() {
if(isActive)
{
prev_time += time_in_miliseconds;
newHandler.removeCallbacks(updateTimerThread);
start_button.setText("Start");
//time_view.setText(""+save_hour+":"+String.format("%2d",save_min)+":"+String.format("%2d",save_sec)+"."+String.format("%3d",save_centisec));
}
else
{
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);
}
isActive = !isActive;
}
};
@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);
});
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;
});
您的计时器需要一些状态。你可以这样做(添加 '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
}
}
};
或者使用状态模式。
我正在尝试构建一个秒表,我有一个名为 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() {
private boolean isActive;
@Override
public void run() {
if(isActive)
{
prev_time += time_in_miliseconds;
newHandler.removeCallbacks(updateTimerThread);
start_button.setText("Start");
//time_view.setText(""+save_hour+":"+String.format("%2d",save_min)+":"+String.format("%2d",save_sec)+"."+String.format("%3d",save_centisec));
}
else
{
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);
}
isActive = !isActive;
}
};
@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);
});
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;
});
您的计时器需要一些状态。你可以这样做(添加 '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
}
}
};
或者使用状态模式。