简体   繁体   English

Android TextView Timer

[英]Android TextView Timer

For my Android application there is a timer that measures how much time has passed. 对于我的Android应用程序,有一个计时器可以测量已经过了多长时间。 Ever 100 milliseconds I update my TextView with some text like "Score: 10 Time: 100.10 seconds". 在100毫秒内,我用一些文本更新了我的TextView,例如“得分:10时间:100.10秒”。 But, I find the TextView only updates the first few times. 但是,我发现TextView仅在前几次更新。 The application is still very responsive, but the label will not update. 该应用程序仍然非常敏感,但标签不会更新。 I tried to call .invalidate(), but it still does not work. 我试图调用.invalidate(),但它仍然无效。 I don't know if there is some way to fix this, or a better widget to use. 我不知道是否有某种方法可以解决这个问题,或者使用更好的小部件。

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);
}

The general solution is to use android.os.Handler instead which runs in the UI thread. 一般的解决方案是使用android.os.Handler而不是在UI线程中运行。 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: 几年前写了一篇关于这个主题的博客文章:

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

What I think is happening is you're falling off the UI thread. 我认为正在发生的是你正在脱离UI线程。 There is a single "looper" thread which handles all screen updates. 有一个“looper”线程可以处理所有屏幕更新。 If you attempt to call "invalidate()" and you're not on this thread nothing will happen. 如果你试图调用“invalidate()”并且你不在这个线程上就不会发生任何事情。

Try using "postInvalidate()" on your view instead. 请尝试在视图中使用“postInvalidate()”。 It'll let you update a view when you're not in the current UI thread. 当您不在当前UI线程中时,它将允许您更新视图。

More info here 更多信息在这里

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);
        }
    }

There is one more way to change text each second; 还有一种方法可以每秒更改文本; this is ValueAnimator . 这是ValueAnimator Here is my solution: 这是我的解决方案:

  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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM