Java Android倒计时-添加时间会导致多个计时器运行

Java Android倒计时-添加时间会导致多个计时器运行,java,android,timer,Java,Android,Timer,我试着让我的计时器在每次按下按钮时增加5秒。我了解到,我需要取消上一个计时器,并创建一个新的计时器,使其按我希望的方式工作。当我按下按钮一次时,计时器加上5秒,一切正常。当我多次按下按钮时,问题就出现了。计时器将在多个不同的计时器之间闪烁,而不是停留在最新的计时器上。每次我按下按钮,另一个计时器在显示屏上闪烁。几乎就好像程序没有取消以前的计时器,而是每次都创建一个新的计时器。我真的很感谢你在这方面的帮助。谢谢大家 import android.os.CountDownTimer; import

我试着让我的计时器在每次按下按钮时增加5秒。我了解到,我需要取消上一个计时器,并创建一个新的计时器,使其按我希望的方式工作。当我按下按钮一次时,计时器加上5秒,一切正常。当我多次按下按钮时,问题就出现了。计时器将在多个不同的计时器之间闪烁,而不是停留在最新的计时器上。每次我按下按钮,另一个计时器在显示屏上闪烁。几乎就好像程序没有取消以前的计时器,而是每次都创建一个新的计时器。我真的很感谢你在这方面的帮助。谢谢大家

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();
    } 
}
改变这个

public void timerCreation (){
    counter.cancel();
    MyCount counter = new MyCount(millisUntilFinishedInt + 5000,1);
    counter.start();
}
进入这个

public void timerCreation (){
    counter.cancel();
    counter = new MyCount(millisUntilFinishedInt + 5000,1);
    counter.start();
}

在当前实现中,将取消成员变量
计数器
。然后创建一个同名的局部变量并启动该变量。下次按下按钮时,您的成员变量
计数器
再次被取消(已取消),以便创建新的
MyCount
对象并启动该对象。这就是为什么你会有多个计时器的原因。

改变:将interal增加到50,但这是强制性的

   MyCount counter = new MyCount(millisUntilFinishedInt + totalAddedTime, 50);
将文本视图重力向左对齐
android:gravity=“left”
,这样用户就感觉不到它的新计时器了

测试工作演示

main活动

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();
    }
}
活动\u 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>

这项工作完美无瑕。非常感谢你。真不敢相信这是如此简单的修复。