问题
Im trying to make it so that my timer adds 5 seconds every time I press the button. I've learnt that I need to cancel the previous timer and create a new one for it to work the way I want it to. When I press the button once, the timer adds 5 seconds and everything works fine as its supposed to. My problem arises when I press the button multiple times. The timer will flicker between many different timers instead of staying on the latest one. Every time I press the button, another timer is flickering on the display. Its almost as if the program doesnt cancel the previous timers and just creates a new every time. I'd really appreciate some help on this. Thanks guys!
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
String countDownTimer;
long millisUntilFinishedInt = 5000;
long milliseconds;
long seconds;
long totalAddedTime = 0;
TextView text1;
MyCount counter = new MyCount(millisUntilFinishedInt + totalAddedTime,17);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(TextView)findViewById(R.id.team_a_score);
counter.start();
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
millisUntilFinishedInt = millisUntilFinished;
seconds = millisUntilFinishedInt/1000;
milliseconds = millisUntilFinishedInt-(millisUntilFinishedInt/1000)*1000;
countDownTimer = "TIME: " + seconds + "." + milliseconds ;
text1.setText(countDownTimer);
}
@Override
public void onFinish() {
countDownTimer = "TIME'S UP!";
text1.setText(countDownTimer);
}
}
public void timerCreation (){
counter.cancel();
MyCount counter = new MyCount(millisUntilFinishedInt + 5000,1);
counter.start();
}
//method that is called when button is pressed
public void threePoints (View v) {
timerCreation();
}
}
回答1:
Change this
public void timerCreation (){
counter.cancel();
MyCount counter = new MyCount(millisUntilFinishedInt + 5000,1);
counter.start();
}
Into this
public void timerCreation (){
counter.cancel();
counter = new MyCount(millisUntilFinishedInt + 5000,1);
counter.start();
}
With your current implementation, you are cancelling your member variable counter. Then you create a local variable with the same name and start that one. The next time you press your button, your member variable counter gets cancelled again (which is already cancelled) in order to create a new MyCount object and start that one. That is why you are ending up with multiple timers
回答2:
Changes: increase interal to 50 but but mandatory.
MyCount counter = new MyCount(millisUntilFinishedInt + totalAddedTime, 50);
Align text view gravity to left android:gravity="left" so user cant feel its new timmer.
Tested Working Demo
MainActivity
public class MainActivity extends AppCompatActivity {
Context context;
int scoreTeamA = 0;
String countDownTimer;
long millisUntilFinishedInt = 5000;
long milliseconds;
long seconds;
long totalAddedTime = 0;
TextView text1;
MyCount counter = new MyCount(millisUntilFinishedInt + totalAddedTime, 50);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
text1 = (TextView) findViewById(R.id.foodName);
text1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timerCreation();
}
});
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
millisUntilFinishedInt = millisUntilFinished;
seconds = millisUntilFinishedInt / 1000;
milliseconds = millisUntilFinishedInt - (millisUntilFinishedInt / 1000) * 1000;
countDownTimer = "TIME: " + seconds + "." + milliseconds;
text1.setText(countDownTimer);
}
@Override
public void onFinish() {
countDownTimer = "TIME'S UP!";
text1.setText(countDownTimer);
}
}
public void timerCreation() {
counter.cancel();
MyCount counter = new MyCount(millisUntilFinishedInt + 5000, 1);
counter.start();
}
//method that is called when button is pressed
public void threePoints(View v) {
timerCreation();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@color/white"
android:orientation="horizontal"
android:weightSum="10">
<TextView
android:id="@+id/foodName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:hint="Food name"
android:gravity="left"
android:inputType="textCapWords"
android:textColor="@color/colorPrimaryDark"
android:textColorHint="@color/colorPrimaryDark"
android:textSize="32sp"
android:layout_marginLeft="20dp" />
</RelativeLayout>
来源:https://stackoverflow.com/questions/39264549/android-countdowntimer-adding-time-results-in-multiple-timers-running