[embedyt] https://www.youtube.com/watch?v=RLnb4vVkftc[/embedyt]
activity_main.xmlMainActivity.java
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context="com.codinginflow.chronometerexample.MainActivity"> <Chronometer android:id="@+id/chronometer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="startChronometer" android:text="Start" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="pauseChronometer" android:text="Pause" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="resetChronometer" android:text="Reset" /> </LinearLayout>
package com.codinginflow.chronometerexample; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Chronometer; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Chronometer chronometer; private long pauseOffset; private boolean running; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chronometer = findViewById(R.id.chronometer); chronometer.setFormat("Time: %s"); chronometer.setBase(SystemClock.elapsedRealtime()); chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer chronometer) { if ((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 10000) { chronometer.setBase(SystemClock.elapsedRealtime()); Toast.makeText(MainActivity.this, "Bing!", Toast.LENGTH_SHORT).show(); } } }); } public void startChronometer(View v) { if (!running) { chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset); chronometer.start(); running = true; } } public void pauseChronometer(View v) { if (running) { chronometer.stop(); pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase(); running = false; } } public void resetChronometer(View v) { chronometer.setBase(SystemClock.elapsedRealtime()); pauseOffset = 0; } }