有关我的Android应用程序存在测量多少时间已经过去了一个计时器。自从100毫秒我喜欢一些文字更新我的TextView“成绩:10时间:100.10秒”。但是,我发现的TextView只更新第几次。该应用程序仍然是非常敏感,但标签不会更新。我试着打电话给.invalidate(),但它仍然无法正常工作。我不知道是否有某种方式来解决这个问题,或者更好的插件使用。

下面是我的代码的示例:

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);
}
有帮助吗?

解决方案

的一般解决方案是使用android.os.Handler代替这在UI线程中运行。它只做一锤子回调,所以你必须每次回调被调用时再次触发它。但它是很容易使用。有关这个主题的一篇博客文章是几年前写的:

http://android-developers.blogspot.com/一十一分之二千零七/缝合为time.html

其他提示

我认为正在发生的事情是你脱落的UI线程。有一个单一的“尺蠖”线程处理所有的屏幕更新。如果你尝试调用“无效()”,你是不是对这个线程什么也不会发生。

尝试使用 “postInvalidate()” 在您的视图代替。它会告诉你,当在当前UI线程是不是你更新视图。

更多信息这里

使用下面的代码来设置时间上的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);
        }
    }

有是改变文本的每个第二一种多方式;这是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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top