-1
我有一個問題,其中我的代碼將不會顯示onTick方法,每次計數器運行時,它只是顯示文本「完成」,因爲它應該在它完成時。看來endTime變量沒有被傳遞給定時器類?任何幫助與我失蹤將不勝感激。倒數計時器不會顯示onTick
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");
}
}