wang_peng1 发表于 2013-1-15 02:19:36

计时表stop watch

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
页: [1]
查看完整版本: 计时表stop watch