Android - 计时器布局没有得到更新

时间:2016-03-31 08:37:50

标签: android

我是android新手。我正在学习创建一个简单的stop watch应用。我得到了三个buttons和一个textview的布局。当我点击start按钮时,它将启动计时器。

单一布局和单一活动。

  public void startTimer(View view){
    running = true;
    TextView textView = (TextView) findViewById(R.id.timer);
    while(running) {
        int hours = seconds / 3600;
        int minutes = (seconds % 60) / 60;
        int sec = seconds % 60;
        String time = String.format("%02d:%02d:%02d", hours, minutes, seconds);
        textView.setText(time);
        seconds++;
        if(seconds == 10){
            running = false;
        }
    }
}

这是我点击start按钮时调用的方法。我调试了代码。值正在正确生成。但不是更新布局。

有什么建议吗?

我得到最后的结果,一个接一个地增加以下添加

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stop_watch);
    runTimer(); //I added this extra.
   }

2 个答案:

答案 0 :(得分:1)

我会为您提供替代解决方案,

 public void startTimer(View view){
    new CountDownTimer(10000, 1000) { //For 10 seconds

        public void onTick(long seconds) {

            String time = String.format("%02d : %02d ",
                    TimeUnit.MILLISECONDS.toMinutes(seconds),
                    TimeUnit.MILLISECONDS.toSeconds(seconds) -
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(seconds))
            );
            textView.setText(time);
        }
        }

        public void onFinish() {
            textView.setText("Finished");
        }
    }.start();
}

更新:

public void startTimer(View view) {
    running = true;
    seconds = 0;
    textView = (TextView) findViewById(R.id.txtView);
    new Thread(new Runnable() {
        @Override
        public void run() {
            while(running) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        int hours = seconds / 3600;
                        int minutes = (seconds % 60) / 60;
                        int sec = seconds % 60;

                         time = String.format("%02d:%02d:%02d", hours, minutes, seconds);
                        textView.setText(time);
                        seconds++;
                    }
                }) ;

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if(seconds==10){
                running=false;
            }
        }
    }).start();

}

答案 1 :(得分:0)

尝试使用以下代码

public void startTimer(View view){
    running = true;
    TextView textView = (TextView) findViewById(R.id.timer);
    while(running) {
        int hours = seconds / 3600;
        int minutes = (seconds % 60) / 60;
        int sec = seconds % 60;
        String time = String.format("%02d:%02d:%02d", hours, minutes, seconds);

        seconds++;

      runOnUiThread(new Runnable() {
             @Override
             public void run() {
               textView.setText(time);
             }
        });

        Thread.sleep(1000)

        if(seconds == 10){
            running = false;
        }
    }
}