Java Android TextView 计时器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/522800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:46:53  来源:igfitidea点击:

Android TextView Timer

javaandroidtimertextview

提问by Isaac Waller

For my Android application there is a timer that measures how much time has passed. Ever 100 milliseconds I update my TextView with some text like "Score: 10 Time: 100.10 seconds". But, I find the TextView only updates the first few times. The application is still very responsive, but the label will not update. I tried to call .invalidate(), but it still does not work. I don't know if there is some way to fix this, or a better widget to use.

对于我的 Android 应用程序,有一个计时器可以测量已经过去了多少时间。每 100 毫秒,我都会用一些文本更新我的 TextView,比如“分数:10 时间:100.10 秒”。但是,我发现 TextView 只更新前几次。该应用程序仍然非常敏感,但标签不会更新。我试图调用 .invalidate(),但它仍然不起作用。我不知道是否有办法解决这个问题,或者有更好的小部件可以使用。

Here is a example of my code:

这是我的代码示例:

float seconds;
java.util.Timer gametimer;
void updatecount() { TextView t = (TextView)findViewById(R.id.topscore);
t.setText("Score: 10 - Time: "+seconds+" seconds");
t.postInvalidate();
}
public void onCreate(Bundle sis) {
... Load the UI, etc...
  gametimer.schedule(new TimerTask() { public void run() {
     seconds+=0.1; updatecount();
} }, 100, 100);
}

采纳答案by Ben Bederson

The general solution is to use android.os.Handler instead which runs in the UI thread. It only does one-shot callbacks, so you have to trigger it again every time your callback is called. But it is easy enough to use. A blog post on this topic was written a couple of years ago:

一般的解决方案是使用 android.os.Handler 而不是在 UI 线程中运行。它只执行一次性回调,因此每次调用回调时都必须再次触发它。但它很容易使用。几年前写了一篇关于这个主题的博客文章:

http://android-developers.blogspot.com/2007/11/stitch-in-time.html

http://android-developers.blogspot.com/2007/11/stitch-in-time.html

回答by haseman

What I think is happening is you're falling off the UI thread. There is a single "looper" thread which handles all screen updates. If you attempt to call "invalidate()" and you're not on this thread nothing will happen.

我认为正在发生的事情是您正在脱离 UI 线程。有一个处理所有屏幕更新的“looper”线程。如果您尝试调用“invalidate()”并且您不在此线程上,则不会发生任何事情。

Try using "postInvalidate()" on your view instead. It'll let you update a view when you're not in the current UI thread.

尝试在您的视图上使用“postInvalidate()”。当您不在当前 UI 线程中时,它会让您更新视图。

More info here

更多信息在这里

回答by Jotiram Chavan

Use Below code to set time on TextView

使用下面的代码在 TextView 上设置时间

public class MyCountDownTimer extends CountDownTimer {
        public MyCountDownTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {

            ExamActivity.this.submitresult();
        }

        @Override
        public void onTick(long millisUntilFinished) {

            long millis = millisUntilFinished;

            int seconds = (int) (millis / 1000) % 60;
            int minutes = (int) ((millis / (1000 * 60)) % 60);
            int hours = (int) ((millis / (1000 * 60`enter code here` * 60)) % 24);

            String ms = String
                    .format("%02d:%02d:%02d", hours, minutes, seconds);
            txtimedisplay.setText(ms);
        }
    }

回答by Volodymyr

There is one more way to change text each second; this is ValueAnimator. Here is my solution:

还有一种每秒更改文本的方法;这是ValueAnimator。这是我的解决方案:

  long startTime = System.currentTimeMillis();
  ValueAnimator animator = new ValueAnimator();
            animator.setObjectValues(0, 1000);
            animator.setDuration(1000);
            animator.setRepeatCount(ValueAnimator.INFINITE);
            animator.addListener(new AnimatorListenerAdapter() {

                @Override
                public void onAnimationStart(Animator animation) {
                    long currentTime = System.currentTimeMillis();
                    String text = TimeFormatUtils.formatTime(startTime - currentTime);
                   yourTextView.setText(text);
                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                    long currentTime = System.currentTimeMillis();
                    String text = TimeFormatUtils.formatTime(startTime - currentTime);
                    yourTextView.setText(text);
                }
            });
            animator.start();