public class MainActivity extends Activity {
private CountDownTimer countDownTimer;
private CountDownTimer countDownTimer2;//second timer count down
private boolean timerStart = false;//timer start
private boolean timerStart2 = false;
private Button startButton;//timer button
private Button startButton2;
public TextView text;
public TextView text2;//text field for second timer
public TextView text3;// text 3 and 4 meant to hold the elapsed time for each timer
public TextView text4;
private final long startTime = 90 * 1000;//first timer
private final long interval = 1 * 1000;
private final long startTime2 = 30 * 1000; //second timer time
private final long interval2 = 1 * 1000;
TextToSpeech tts;
private class countDownTimerListener implements OnClickListener {
@Override
public void onClick(View v) {
if (!timerStart){
countDownTimer.start();
timerStart = true;
startButton.setText("Stop");
startButton2.setEnabled(false);
} else {
countDownTimer.cancel();
timerStart = false;
startButton.setText("Restart");
}
}
}
private class countDownTimerListener2 implements OnClickListener {
@Override
public void onClick(View v) {
if (!timerStart2){
countDownTimer2.start();
timerStart2 = true;
startButton2.setText("Stop");
startButton.setEnabled(false);
} else {
countDownTimer2.cancel();
timerStart2 = false;
startButton2.setText("Restart");
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button)this.findViewById(R.id.button1);
startButton2 = (Button)this.findViewById(R.id.button2);
startButton.setOnClickListener(new countDownTimerListener());
startButton2.setOnClickListener(new countDownTimerListener2());
text = (TextView) this.findViewById(R.id.textView1);
text2 = (TextView) this.findViewById(R.id.textView4);//second timer text
text3 = (TextView) this.findViewById(R.id.textView5);//text field for elapsed time
text4 = (TextView) this.findViewById(R.id.textView7);
//this is where I take the text view from xml and assign to text 3 and 4.
countDownTimer = new MyTimer(startTime, interval, "You have completed your 90 second Timer", text, text3);//first timer
text.setText(text.getText() + String.valueOf(startTime/1000));
countDownTimer2 = new MyTimer(startTime2, interval2, "You have completed your 30 second Timer", text2, text4);//second timer start
text2.setText(text2.getText() + String.valueOf(startTime2/1000));//second timer text field
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR){
tts.setLanguage(Locale.US);
}
}
});
}
public class MyTimer extends CountDownTimer {
private String ttsString;
private TextView textview;
private TextView timeElapsedView;
private long timeElapsed;
private long startTime;
public MyTimer(long startTime, long interval, String ttsString, TextView textview, TextView timeElapsedView){
super(startTime, interval);
this.ttsString = ttsString;
this.textview = textview;
this.timeElapsedView = timeElapsedView;
}
@Override
public void onTick(long millisUntilFinished) {
textview.setText("" + (millisUntilFinished / 1000));
//startTime - millisUntilFinished elapsed time
timeElapsed = ((startTime - millisUntilFinished) / 1000);
timeElapsedView.setText("" + String.valueOf(timeElapsed));
}
@Override
public void onFinish() {
textview.setText("Time's up!");
//Vibrator vibrator = (Vibrator)this.getSystemService(VIBRATOR_SERVICE);
//vibrator.vibrate(600);
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 100);
toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD, 200);
tts.speak(ttsString, TextToSpeech.QUEUE_ADD, null);
}
}
}
我正在尝试用Java为Android创建一个倒数计时器。我希望它显示从1开始的经过时间,以及倒计时的剩余时间。我可以让倒数时间工作正常但是当我经过时间时,它从89971开始并从那里开始计数。关于如何解决这个问题的任何建议?这是我用来显示计时器的代码。
答案 0 :(得分:1)
startTime
方法中的onTick()
来自哪里?如果它是您MyTimer
级的实例变量,则您忘记提及它。此外,它是一个很长的,以毫秒为单位的时间值,就像millisInFuture
- 值一样。这意味着,你应该做这样的事情:
timeElapsed = (startTime - millisUntilFinished) / 1000;
您可以将timeElapsed
作为onTick()
- 方法的局部变量。
披露完整代码后更新:
请在startTime
- 类中添加MyTimer
实例变量,您可以在MyTimer
- 构造函数中正确初始化,因为您在两个不同的开始时间使用MyTimer,但在onTick()
中,您使用外部类中的startTime
实例变量,该变量仅对第一个实例(countDownTimer
)有效。