Answer the question
In order to leave comments, you need to log in
Stopwatch class?
I am writing an application in which there are 2 stopwatches and the corresponding start / stop / pause, what class should I use for the stopwatch?
Answer the question
In order to leave comments, you need to log in
package com.navikolus.timerExample;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Chronometer;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Chronometer mChronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mChronometer = (Chronometer) findViewById(R.id.chronometer);
mChronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
long elapsedMillis = SystemClock.elapsedRealtime()
- mChronometer.getBase();
if (elapsedMillis > 5000) {
String strElapsedMillis = "Прошло больше 5 секунд";
Toast.makeText(getApplicationContext(),
strElapsedMillis, Toast.LENGTH_SHORT)
.show();
}
}
});
}
public void onStartClick(View view) {
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.start();
}
public void onStopClick(View view) {
mChronometer.stop();
}
public void onResetClick(View view) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question