有一段时间我无法弄清问题是什么,我不知道在哪里寻找答案,所以我问你们。我想为用户数据输入的间隔创建计时器(训练时间,休息时间,重复次数)。我希望它能像这样工作: 有2个文本视图显示:训练时间的计时器,休息时间的计时器) 第一个运行是训练时间,直到训练输入数据的时间为止。 然后休息时间正在运行。 所有这些都在for循环中工作(直到完成重复次数)。 但是,当我按下启动按钮时,它会冻结。 Graddle没有显示错误。
编辑: 我发现这是while循环的问题(我有三个)。但是这个while循环已经可以运行了,应该可行。我应该创建新的Runnable并使其像线程?我尝试过这个,但它仍然没有用。我不知道怎么能让它发挥作用。
这是我的代码:
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.os.Handler;
public class Interval extends AppCompatActivity {
private TextView timerValue;
private TextView timerValue2;
private TextView repetition;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeV, timeB, train, tbreak;
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
int reps;
Button resetButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interval);
train = getIntent().getLongExtra("train", 0);
tbreak = getIntent().getExtras().getLong("tbreak", 0);
reps = getIntent().getIntExtra("reps", 0);
resetButton = (Button) findViewById(R.id.button3);
resetButton.setEnabled(false);
timerValue = (TextView) findViewById(R.id.textView4);
timerValue2 = (TextView) findViewById(R.id.textView5);
repetition = (TextView) findViewById(R.id.textView6);
repetition.setText("0/" + reps);
}
public void startButton(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
resetButton.setEnabled(false);
}
public void pauseButton(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
resetButton.setEnabled(true);
}
public void resetButton(View view) {
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
for (int x=1; x<reps; x++) {
long y = (long)x;
repetition.setText("" + x + "/" + reps);
timerValue.setText("0:00:000");
timerValue2.setText("0:00:000");
timeV = updatedTime - (y - 1) * (train + tbreak);
while (timeV <= train) {
int secs = (int) (timeV / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (timeV % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
}
while (train < timeV && timeV <= (train + tbreak)) {
timerValue.setText("0:00:000");
timeB = timeV - train;
int secs = (int) (timeB / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (timeB % 1000);
timerValue2.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
}
if (x==reps-1){
timeV = updatedTime - y * (train + tbreak);
while (timeV <= train) {
int secs = (int) (timeV / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (timeV % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
}
}
}
customHandler.postDelayed(this, 0);
}
};
}