문제

내 안드로이드 프로그램 타이머가 있을 측정하는 얼마나 시간이 지났습니다.적 100 밀리초 단위 업데이트 스트링과 텍스트"와 같은 점수:10 시간:100.10 초이다."하지만,나를 찾아 뷰 업데이트만 처음 몇 번입니다.응용 프로그램은 여전히 반응하지만,레지 않을 것입 업데이트합니다.도 호출합니다.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);
}
도움이 되었습니까?

해결책

일반적인 솔루션을 사용하는 것이다.os.핸들러 대신에서 실행됩니다.그것은 단지 않는 한-콜백을 촬영,그래서 당신은 그것을 실행시 콜백을 호출됩니다.하지만 그것은 쉽게 충분히 사용합니다.블로그 포스팅에 이 항목에 작성되었고 몇 년 전에:

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

다른 팁

내가 무슨 생각이 일어나고 있는 것은 당신이 떨어지는니다.단일"루"실 처리하는 모든 화면을 업데이트됩니다.는 경우에 당신을 호출하려고 시도"invalidate()"당신이 이 스레드에서 아무 일도 일어나지 않을 것입니다.

도를 사용하여"postInvalidate()"에서 보기 대신 합니다.그것은 당신에게 업데이트 보기지 않을 때에 현재니다.

더 많은 정보

아래에 사용하는 코드 설정된 시간에서 뷰

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