对 CountDownTimer 有疑问。我必须制作一个应用程序,允许用户在每次单击按钮时将时间时间增加 +1。然后在按钮停止单击后等待三秒钟,然后开始倒计时。
我在下面粘贴了我的代码。
我的问题是:我似乎无法使数字的递增正常工作,但是似乎在我停止递增数字(onStop())后,它直接转到(onFinish())。而不是去 OnTick() 并每秒将数字减少 1。我尝试了很多方法来解决这个问题,但都被卡住了。
谁能带领我朝着正确的方向前进?任何帮助,将不胜感激。谢谢你们!
public class MainActivity extends Activity {
Button incrementTime, startTime;
public TextView timedisplay;
public myTimer wavetimer;
private long millisInFuture;
private long millisUntilFinished;
private long countDownInterval;
private long onclicktime;
//private WaveInterface model;
public int countdown;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
incrementTime = (Button) findViewById(R.id.button2);
startTime = (Button) findViewById(R.id.button3);
incrementTime.setText("Increment Time");
startTime.setText("Start");
timedisplay = (TextView) findViewById(R.id.mycounter);
timedisplay.setText("Time Left: " + millisUntilFinished);
wavetimer = new myTimer (millisInFuture, 1000);
incrementTime.setOnClickListener(new OnClickListener(){
int countdown = 01;
/**
* On click button Listner
* @return
*/
public void onClick(View v) {
millisInFuture++;
return;
}
});
startTime.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
wavetimer.start();
}
});}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class myTimer extends CountDownTimer {
public long millisInFuture;
private long countDownInterval;
public myTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
millisUntilFinished--;
if (millisUntilFinished == 0){
wavetimer.onFinish();
}
else {
timedisplay.setText("Time Left: " + millisUntilFinished / 1000);
}
}
@Override
public void onFinish() {
timedisplay.setText("Countdown Finished");
wavetimer.cancel();
}
public void onStop() {
millisInFuture = millisInFuture + 1;
timedisplay.setText("Time Left: " + millisInFuture);
}
我似乎无法弄清楚如何正确增加数字。我尝试了多种方法,它一直在搞砸。不知道在哪里看。任何帮助,将不胜感激。谢谢你们!