六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 47|回复: 0

计时表stop watch

[复制链接]

升级  82%

292

主题

292

主题

292

主题

进士

Rank: 4

积分
910
 楼主| 发表于 2013-1-15 02:19:36 | 显示全部楼层 |阅读模式
import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class Main extends Activity implements OnClickListener{    final int MSG_START_TIMER = 0;    final int MSG_STOP_TIMER = 1;    final int MSG_UPDATE_TIMER = 2;    Stopwatch timer = new Stopwatch();    final int REFRESH_RATE = 100;    Handler mHandler = new Handler()    {        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            switch (msg.what) {            case MSG_START_TIMER:                timer.start(); //start timer                mHandler.sendEmptyMessage(MSG_UPDATE_TIMER);                break;            case MSG_UPDATE_TIMER:                tvTextView.setText(""+ timer.getElapsedTime());                mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER,REFRESH_RATE); //text view is updated every second,                 break;                                  //though the timer is still running            case MSG_STOP_TIMER:                mHandler.removeMessages(MSG_UPDATE_TIMER); // no more updates.                timer.stop();//stop timer                tvTextView.setText(""+ timer.getElapsedTime());                break;            default:                break;            }        }    };    TextView tvTextView;    Button btnStart,btnStop;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        tvTextView = (TextView)findViewById(R.id.TextView01);        btnStart = (Button)findViewById(R.id.Button01);        btnStop= (Button)findViewById(R.id.Button02);        btnStart.setOnClickListener(this);        btnStop.setOnClickListener(this);    }    public void onClick(View v) {        if(btnStart == v)        {            mHandler.sendEmptyMessage(MSG_START_TIMER);        }else        if(btnStop == v){            mHandler.sendEmptyMessage(MSG_STOP_TIMER);        }    }} 
/*    Copyright (c) 2005, Corey Goldberg    StopWatch.java is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.*/public class StopWatch {        private long startTime = 0;    private long stopTime = 0;    private boolean running = false;        public void start() {        this.startTime = System.currentTimeMillis();        this.running = true;    }        public void stop() {        this.stopTime = System.currentTimeMillis();        this.running = false;    }        //elaspsed time in milliseconds    public long getElapsedTime() {        long elapsed;        if (running) {             elapsed = (System.currentTimeMillis() - startTime);        }        else {            elapsed = (stopTime - startTime);        }        return elapsed;    }            //elaspsed time in seconds    public long getElapsedTimeSecs() {        long elapsed;        if (running) {            elapsed = ((System.currentTimeMillis() - startTime) / 1000);        }        else {            elapsed = ((stopTime - startTime) / 1000);        }        return elapsed;    }               } http://www.goldb.org/stopwatchjava.html
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表