倒数计时器不会显示onTick

时间:2017-06-18 07:22:25

标签: java android countdowntimer

我遇到的问题是我的代码不会显示onTick方法,每次运行计数器时,它只显示文本“Done”,就像它完成时一样。似乎endTime变量没有传递给计时器类?我将不胜感激任何帮助。

public class MainActivity extends AppCompatActivity {

private long endTime;
private MyTimer mainTimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Set up the two number pickers
    NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes);
    pickerSetup(minutes);
    NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds);
    pickerSetup(seconds);

    mainTimer = new MyTimer(this, endTime, 1000);
}

public void pickerSetup(NumberPicker pickerId){
    pickerId.setMaxValue(60);
    pickerId.setMinValue(0);
    pickerId.setWrapSelectorWheel(true);
}

public void calcTime(View view){
    //Get value of Number pickers and convert to milliseconds from minutes and seconds.
    NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes);
    int selectedMinutes = minutes.getValue() * 60000;

    NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds);
    int selectedSeconds = seconds.getValue() * 1000;

    //Add selected Minutes and seconds together
    endTime = selectedMinutes + selectedSeconds;
    Log.v("End time = ", String.valueOf(endTime));
    mainTimer.start();
 }
}

计时器课程:

class MyTimer extends CountDownTimer {

private Context mContext;
private TextView mToUpdate;


public MyTimer(Context context, long startTime, long interval) {
    super(startTime, interval);
    mContext = context;
    TextView toUpdate = (TextView) ((Activity)mContext).findViewById(R.id.intervalTimer);
    mToUpdate = toUpdate;
}

@Override
public void onTick(long millisUntilFinished) {
    int seconds = (int) (millisUntilFinished / 1000) % 60;
    int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
    mToUpdate.setText(String.valueOf(minutes) + ":" + String.valueOf(seconds));
}

@Override
public void onFinish() {
    mToUpdate.setText("Done");
}

}

1 个答案:

答案 0 :(得分:0)

这是因为您在调用mainTimer = new MyTimer(this, endTime, 1000);时没有初始化endTime。它设置为0,这就是它作为启动停止的原因。您可以将这两种方法更改为这样的方法。

public long calcTime(View view){
    //Get value of Number pickers and convert to milliseconds from minutes and seconds.
    NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes);
    int selectedMinutes = minutes.getValue() * 60000;

    NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds);
    int selectedSeconds = seconds.getValue() * 1000;

    //Add selected Minutes and seconds together
    endTime = selectedMinutes + selectedSeconds;
    Log.v("End time = ", String.valueOf(endTime));
    return mainTimer.start();
 }

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Set up the two number pickers
    NumberPicker minutes = (NumberPicker) findViewById(R.id.minutes);
    pickerSetup(minutes);
    NumberPicker seconds = (NumberPicker) findViewById(R.id.seconds);
    pickerSetup(seconds);

    endTime=calcTime();
    mainTimer = new MyTimer(this, endTime, 1000);
    mainTimer.start();
}