为什么在Android Studio中onFinish会运行多次?

huangapple 未分类评论43阅读模式
英文:

Why does onFinish run multiple times in android studio?

问题

我创建了一个按钮来计算点击次数,并创建了一个5秒的定时器。在5秒后,我想通过将计数器除以5来找到每秒点击次数(cps)。然而,问题在于,当我将计数器重置为0后,用于找到cps的代码仍然在onFinish方法中运行,并且会使cps也变为0。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mtextViewCountDown = (TextView) findViewById(R.id.text_view_countdown);
    btnClick = (Button) findViewById(R.id.button2);
    displayCPS = (TextView) findViewById(R.id.cps);

    btnClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startTimer();
            counter++;
            btnClick.setText(Integer.toString(counter));
        }
    });
}

public void startTimer(){
    countDownTimer = new CountDownTimer(timeLeft,1000){
        public void onTick(long l){
            timeLeft = l;
            updateTimer();
        }
        public void onFinish() {
            cps = counter / 5;
            System.out.println(cps);
            displayCPS.setText("CPS: " + Double.toString(cps));
        }
    }.start();
}

public void updateTimer(){
    int minutes = 0;
    int seconds = (int) timeLeft / 1000;
    String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
    mtextViewCountDown.setText(timeLeftFormatted);
}

public void resetTimer(){
    counter = 0;
    btnClick.setText(Integer.toString(0));
    timeLeft = 5000;
}

我尝试进行了调试并在控制台中打印出cps。

I/System.out: 5.0
I/System.out: 0.0
I/chatty: uid=10088(com.example.cpspractice) identical 14 lines
I/System.out: 0.0
I/System.out: 0.0
I/chatty: uid=10088(com.example.cpspractice) identical 10 lines
I/System.out: 0.0

看起来好像onFinish方法运行了多次。你们是否有办法让它只运行一次?

英文:

I created a button that counts clicks and created a timer for 5 seconds. After the 5 seconds, I want to find the clicks per second (cps) by taking counter and dividing it by 5. However, the problem is that after I reset counter to 0, the code for finding cps still runs in the onFinish method and makes the cps also to 0.

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mtextViewCountDown = (TextView) findViewById(R.id.text_view_countdown);
        btnClick = (Button) findViewById(R.id.button2);
        displayCPS = (TextView) findViewById(R.id.cps);

        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {

                startTimer();
                counter++;
                btnClick.setText(Integer.toString(counter));

            }
        });
    }
    public void startTimer(){
        countDownTimer = new CountDownTimer(timeLeft,1000){
            public void onTick(long l){

                timeLeft = l;
                updateTimer();
            }
            public void onFinish() {

                cps = counter / 5;
                System.out.println(cps);
                displayCPS.setText("CPS: " + Double.toString(cps));


            }
        }.start();
    }
    public void updateTimer(){
        int minutes = 0;
        int seconds = (int) timeLeft / 1000;
        String timeLeftFormatted = String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);
        mtextViewCountDown.setText(timeLeftFormatted);

    }
    public void resetTimer(){
        counter = 0;
        btnClick.setText(Integer.toString(0));
        timeLeft = 5000;

    }

I tried to debug it and print out the cps in console.

I/System.out: 5.0
I/System.out: 0.0
I/chatty: uid=10088(com.example.cpspractice) identical 14 lines
I/System.out: 0.0
I/System.out: 0.0
I/chatty: uid=10088(com.example.cpspractice) identical 10 lines
I/System.out: 0.0

It seems to be like onFinish runs multiple times. Do you guys have any idea on how to make it only run once?

答案1

得分: 0

如果我没有弄错的话,定时器会被多次执行,而onFinish()会在每次定时器执行完成后被执行。这只是我的一个猜测,因为从你的代码片段中无法得出这一点。

英文:

If I am not mistaken, the timer is executed several times and the 'onFinish()` is executed after each pass of the timer. This is just one of my theories, since you can't read that from your code snippet.

答案2

得分: 0

onFinish()只运行一次 - 但是对于你启动的每个定时器都会运行一次。

看起来你在每次点击按钮时都启动了一个定时器,所以如果你点击按钮20次,就会启动20个定时器,每个定时器的onFinish()方法都会被调用一次。

要解决这个问题,只需要在第一次点击时启动一个定时器即可:

btnClick.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        counter++;
        if (counter == 1) {
            startTimer();
        }
        btnClick.setText(Integer.toString(counter));
    }
});
英文:

onFinish() runs once - but once for every timer that you start.

It seems that you start a timer on every click of the button, so if you tap the button 20 times you start 20 timers and each timer has it's onFinish() method called once.

To fix that start only one timer, for example at the first click:

    btnClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            counter++;
            if (counter == 1) {
                startTimer();
            }
            btnClick.setText(Integer.toString(counter));

        }
    });

huangapple
  • 本文由 发表于 2020年4月11日 05:31:22
  • 转载请务必保留本文链接:https://java.coder-hub.com/61148997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定