如何在Android中使用计时器控制?

时间:2012-12-20 08:54:18

标签: android timer

我带两个textview和一个button(开始/一圈/停止)。当我按下开始按钮然后显示圈数文本替换为开始按钮并在第一个textview和倒计时开始时显示小时,分钟和秒。当我按下按钮然后第一个textview的值(小时,分钟,秒)停止并开始倒计时第二个textview上的计时器。最后我按下停止按钮,然后停止所有值(小时,分钟,秒)并开始显示按钮上的文字。

Runnable run = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            long millis = System.currentTimeMillis() - startTime;
            int seconds = (int)(millis/1000);
            int hours =seconds/3600;
            int minutes = (seconds % 3600)/60;
            seconds = seconds % 60;
            ed1.setText(String.format("%02d:%02d:%02d", hours,minutes,seconds));
            h2.postDelayed(this, 500);
        }
    };
    class firstTask extends TimerTask {

        @Override
        public void run() {
            handler.sendEmptyMessage(0);
        }
    };
    class secondTask extends TimerTask{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            LapActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    long millis = System.currentTimeMillis() - startTime;
                    int seconds = (int)(millis/1000);
                    int hours =seconds/3600;
                    int minutes = (seconds % 3600)/60;
                    seconds = seconds % 60;
                    ed2.setText(String.format("%02d:%02d:%02d", hours,minutes,seconds));
                }
            });
        }

    }

0 个答案:

没有答案