Android-时间间隔计时器
有一段时间我不知道问题出在哪里,我也不知道该去哪里寻找答案,所以我在问你们。我想用用户数据输入(训练时间、休息时间、重复次数)为间隔创建计时器。我希望它能像这样工作: 有两个文本视图显示:训练时间计时器、休息时间计时器) 第一次运行是训练时间,直到它与训练输入数据的时间相等。 然后休息时间就开始了。 所有这些都在for循环中工作(直到重复次数结束)。 但当我按下“开始”按钮时,它会冻结。格拉德尔没有出现错误 编辑: 我发现这是while循环的问题(我有三个)。但是这个while循环已经处于可运行状态,应该可以工作了。我应该创建新的Runnable并使其像线程一样吗?我试过了,但还是不行。我不知道怎样才能让它工作 这是我的密码:Android-时间间隔计时器,android,timer,Android,Timer,有一段时间我不知道问题出在哪里,我也不知道该去哪里寻找答案,所以我在问你们。我想用用户数据输入(训练时间、休息时间、重复次数)为间隔创建计时器。我希望它能像这样工作: 有两个文本视图显示:训练时间计时器、休息时间计时器) 第一次运行是训练时间,直到它与训练输入数据的时间相等。 然后休息时间就开始了。 所有这些都在for循环中工作(直到重复次数结束)。 但当我按下“开始”按钮时,它会冻结。格拉德尔没有出现错误 编辑: 我发现这是while循环的问题(我有三个)。但是这个while循环已经处于可运行
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);
}
};
导入android.os.SystemClock;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入android.os.Handler;
公共类间隔扩展了AppCompative活动{
私有文本视图时间值;
私有文本视图时间值2;
私有文本视图重复;
专用长启动时间=0L;
私有处理程序customHandler=新处理程序();
长时间V、时间B、列车、tbreak;
长时间单位毫秒=0L;
长时间WAPBUFF=0L;
长更新时间=0L;
杰出代表;
按钮复位按钮;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interval);
列车=getIntent().getLongExtra(“列车”,0);
tbreak=getIntent().getExtras().getLong(“tbreak”,0);
reps=getIntent().getIntExtra(“reps”,0);
resetButton=(按钮)findViewById(R.id.button3);
resetButton.setEnabled(错误);
timerValue=(TextView)findViewById(R.id.textView4);
timerValue2=(TextView)findViewById(R.id.textView5);
重复=(TextView)findViewById(R.id.textView6);
重复.setText(“0/”+重复);
}
公共无效开始按钮(视图){
startTime=SystemClock.uptimeMillis();
postDelayed(updateTimerThread,0);
resetButton.setEnabled(错误);
}
公共无效暂停按钮(视图){
timeSwapBuff+=timein毫秒;
removeCallbacks(updateTimerThread);
resetButton.setEnabled(真);
}
公共无效重置按钮(视图){
}
private Runnable updateTimerThread=new Runnable(){
公开募捐{
timeinmillides=SystemClock.uptimeMillis()-startTime;
updatedTime=timeSwapBuff+Timein毫秒;
对于(int x=1;x