本文介绍了处理程序更新UI定时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code更新我的UI一个计时器。
与此$ C $然而c中的问题是,与每个第二通过,+1第二更新。
我想我明白的为什么发生这种情况,但我不知道如何解决它。

I use the following code to update a timer in my UI.The problem with this code however, is that with each second passed, it updates by +1 second.I think I understand why this happens, however I don't know how to fix it.

        private Handler mHandler = new Handler();
        ....


    private Runnable mUpdateTimeTask = new Runnable() {
           public void run() {
                                mDifference = System.currentTimeMillis() - mStartTime;
                                workSum = workSum + mDifference;
                                TextViewTime.setText("Time so far: " + formatTime(workSum));
                                mHandler.postDelayed(this, 1000);

           }
        };

        public void onClick(View v) {
                switch (v.getId()) {
                case R.id.start_button:
                startButton.setEnabled(false);
                stopButton.setEnabled(true);

                        if (mStartTime == 0L) {
                    mStartTime = System.currentTimeMillis();
                    mHandler.removeCallbacks(mUpdateTimeTask);
                    mHandler.postDelayed(mUpdateTimeTask, 1000);
                        }

帮助将不胜AP preciated!

Help would be greatly appreciated!

推荐答案

workSum = workSum + mDifference; 好像是你的问题,因为mDifference每次都会递增。
我觉得 workSum = mDifference; 将解决它,甚至

workSum = workSum + mDifference; seems to be your problem, because mDifference is incrementing each time.I think workSum = mDifference; will solve it, or even

mDifference = System.currentTimeMillis() - mStartTime;
TextViewTime.setText("Time so far: " + formatTime(mDifference));

这篇关于处理程序更新UI定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-01 01:18