Explanation of Android timer chronometer using example

  • 2021-07-06 11:49:05
  • OfStack

In Android, you can use a timer to monitor time. This class has the functions of starting timing, stopping timing, re-timing, and setting the timer
Time mode, the prototype of the timer method is listed below:
long getBase (); //Time of return to base, set by setBase (long)
String getFormat (); //Returns the current string format, which is implemented through setFormat ()
void setBase (long base); //Set the time, count the value specified by the timer
void setFormat (String format); //Set what is displayed, and the timer will display the value of this parameter, if the value of the string
If//is null, the value returned is MM: SS format
Here is an example:


package com.example.android.apis.view;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import com.example.android.apis.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;

public class ChronometerDemo extends Activity {
  Chronometer mChronometer;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.chronometer);

    Button button;

    mChronometer = (Chronometer) findViewById(R.id.chronometer);

    // Watch for button clicks.
    button = (Button) findViewById(R.id.start);
    button.setOnClickListener(mStartListener);// Corresponding to the start timing 

    button = (Button) findViewById(R.id.stop);
    button.setOnClickListener(mStopListener); // The corresponding is to stop timing 

    button = (Button) findViewById(R.id.reset);
    button.setOnClickListener(mResetListener);// Corresponding to the reset number 

    button = (Button) findViewById(R.id.set_format);
    button.setOnClickListener(mSetFormatListener);// Corresponding to the display format of setting time 

    button = (Button) findViewById(R.id.clear_format);
    button.setOnClickListener(mClearFormatListener);// Corresponding to the use of non-formatted timing display function 
  }

  View.OnClickListener mStartListener = new OnClickListener() {
    public void onClick(View v) {
      mChronometer.start();
    }
  };

  View.OnClickListener mStopListener = new OnClickListener() {
    public void onClick(View v) {
      mChronometer.stop(); 
    }
  };

  View.OnClickListener mResetListener = new OnClickListener() {
    public void onClick(View v) {
      mChronometer.setBase(SystemClock.elapsedRealtime());
    }
  };

  View.OnClickListener mSetFormatListener = new OnClickListener() {
    public void onClick(View v) {
      mChronometer.setFormat("Formatted time (%s)");
    }
  };

  View.OnClickListener mClearFormatListener = new OnClickListener() {
    public void onClick(View v) {
      mChronometer.setFormat(null);
    }
  };
}

The above is the whole content of this article. I hope it will be helpful for everyone to learn to use Android timer. Thank you for reading.


Related articles: