问题
have a question about CountDownTimer. I have to make an application that allows the user to increment the time time by +1, for every click of the button. Then after the button stops being clicked it waits for three seconds, then starts to countdown.
I pasted my code below.
My Issue is: I can't seem to get the Incrementing of the number working correctly, however it seems that after I stop Incrementing the number (onStop()) it directly goes to (onFinish()). Instead of going to the OnTick() and decreasing the number by 1 every second. I have tried numerous ways to fix this, but have been stuck.
Can anyone lead me in the right direction of what to do? Any help would be appreciated. Thank you guys!
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);
}
I can't seem to figure out how to increment the number correctly. I have tried multiple ways and it keeps screwing up. Not sure where to look. Any help would be appreciated. Thank you guys!
回答1:
You should know that you don't have to manually call onFinish
when time is up in the countdown class, neither do you have you to cancel in onFinish
as one leads to the other or manually decrement anything. Try the following and see if it fits the task that you had in mind. Hope you can note that i don't see pragmatic reason to see if a button "isn't being clicked anymore".
public class MainActivity extends Activity {
Button incrementTime, startTime;
public TextView timedisplay;
long millisInFuture = 1000;
@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);
timedisplay = (TextView) findViewById(R.id.mycounter);
resetText();
incrementTime.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
millisInFuture += 1000;
resetText();
}
});
startTime.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
CountDownTimer wavetimer = new myTimer(millisInFuture + 3000, 1000).start();
// ^ add 3 seconds.
}
});}
protected void resetText() {
timedisplay.setText("Time Left: " + millisInFuture / 1000);
}
public class myTimer extends CountDownTimer {
private long millisActual;
public myTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
millisActual = millisInFuture - 3000;
}
@Override
public void onTick(long millisUntilFinished) {
//v start showing the tick after 3 seconds.
if (millisUntilFinished <= millisActual) {
timedisplay.setText("Time Left: " + millisUntilFinished / 1000);
}
}
@Override
public void onFinish() {
timedisplay.setText("Countdown Finished");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
回答2:
Add the TextView with name 'timerText' in your activity xml file
Add following two global variables in the class
int repeatCounter=1;//incrementing for every 60 sec
CountDownTimer tripTimeCounter;
Add following method to the class & call it from activity onCreate or from any other place where you want
public void startTimeCounter(){
tripTimeCounter=new CountDownTimer(60*1000, 1000){
@Override
public void onFinish() {
// TODO Auto-generated method stub
repeatCounter=repeatCounter+1;
startTimeCounter();//follow the recursion on finish of the limit of 60 seconds & increment the repeat counter
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
timerText.setText(formatMillis((repeatCounter*60)*1000-millisUntilFinished));
}
}.start();
}
To show the value in the time format use following method
static public String formatMillis(long val) {
StringBuilder buf=new StringBuilder(20);
String sgn="";
if(val<0) { sgn="-"; val=Math.abs(val); }
append(buf,sgn,0,( val/3600000));
append(buf,":",2,((val%3600000)/60000));
append(buf,":",2,((val %60000)/1000));
//append(buf,".",3,( val%1000));
return buf.toString();
}
来源:https://stackoverflow.com/questions/13478471/incrementing-the-countdowntimer-android