Chronometer 计时器使用

 1 public class TestProject extends Activity {

 2     TextView textGoesHere;

 3     long startTime;

 4     long countUp;

 5 

 6     @Override

 7     public void onCreate(Bundle savedInstanceState) {

 8         super.onCreate(savedInstanceState);

 9         setContentView(R.layout.main);

10 

11         Chronometer stopWatch = (Chronometer) findViewById(R.id.chrono);

12         startTime = SystemClock.elapsedRealtime();

13 

14         textGoesHere = (TextView) findViewById(R.id.textGoesHere);

15         stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){

16             @Override

17             public void onChronometerTick(Chronometer arg0) {

18                 countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;

19                 String asText = (countUp / 60) + ":" + (countUp % 60); 

20                 textGoesHere.setText(asText);

21             }

22         });

23         stopWatch.start();

24     }

25 }
<Chronometer android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:id="@+id/chrono"

    android:visibility="gone"/>

 

你可能感兴趣的:(计时器)