It may be that we often use timers in development, such as when we are recording, we may need to display a timer in the upper-right corner. This thing is actually very simple to achieve.
Only need to use one control chronometer, yes, it's so simple, I'm embarrassed to tell you about it.
<chronometer
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
android: format= '%s '
android:id= ' @+id/timer '/>
Yes, it's that simple. Java Code Also
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
Timer = (chronometer) Findviewbyid (R.id.timer);
}
public void Btnclick (view view) {
timer.setbase (Systemclock.elapsedrealtime ());//Timer Clear 0
timer.start ();
}
Super simple with wood? Look at the results of the operation:
Perhaps you would say, if you need to show the time how to do it? There is no hurry, two lines of code can solve the problem.
public void Btnclick (view view) {
timer.setbase (Systemclock.elapsedrealtime ());//timer clear 0
int hour = (int) ( Systemclock.elapsedrealtime ()-timer.getbase ())/1000/60);
Timer.setformat ("0" +string.valueof (hour) + ":%s");
Timer.start ();
}
public void Stopclick (view view) {
timer.stop ();
}
Well, yes, it's that simple, but don't forget to change the format of the XML.
<chronometer
android:layout_width= "match_parent"
android:layout_height= "Wrap_content"
android:format= "00:00:00"
android:gravity= "center"
android:id= "@+id/timer"/>
Yes, you did not read wrong, so that can be, do not believe, you see!
It's the same as the time on the video you imagined. Well. You set a circle in front of you, and then set the timer color just like it has the force lattice.
And perhaps you do not like this way, of course, it is also possible to use the Handler+timer+timertask way. Because it's too simple, it's directly on the code.
Package Com.example.nanchen.timerdemo;
Import Android.os.SystemClock;
Import android.support.annotation.Nullable;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Chronometer;
Import Android.widget.TextView;
Import Java.util.Locale;
Import Java.util.Timer;
Import Java.util.TimerTask; public class Mainactivity extends Appcompatactivity {private chronometer timer; private timer timer1; private TextView te
Xtview;
Private TimerTask TimerTask;
@Override protected void OnCreate (@Nullable Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Timer = (chronometer) Findviewbyid (R.id.timer);
TextView = (TextView) Findviewbyid (R.id.text);
Timer1 = new Timer (); public void Btnclick (view view) {Timer.setbase (Systemclock.elapsedrealtime ());//timer clear 0 int hour = (int) (SYSTEMCLOCK.E
Lapsedrealtime ()-timer.getbase ())/1000/60); Timer.setformat ("0" +string.valueof (hour) + ":%S ");
Timer.start (); public void Stopclick (view view) {Timer.stop ():} public void Startclick (view view) {TimerTask = new TimerTask () {int
CNT = 0; @Override public void Run () {Runonuithread (new Runnable () {@Override the public void Run () {Textview.settext () (
cnt++));
}
});
}
};
Timer1.schedule (timertask,0,1000); Private String getstringtime (int cnt) {int hour = cnt/3600 int min = cnt% 3600/60 int second = cnt%; return St
Ring.format (Locale.china, "%02d:%02d:%02d", Hour,min,second); public void StopClick1 (view view) {if (!timertask.cancel ()) {timertask.cancel (); Timer1.cancel ();}}}
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height
= "Match_parent" android:orientation= "vertical" tools:context= "com.example.nanchen.timerdemo.MainActivity" > <chronometer android:layout_width= "match_parent" android:layout_height= wrap_content "android:format=" 00:00:00 "android:gravity=" center "android:id=" @+id/timer "/> <button android:layout_width=" Match_parent "Android:o nclick= "Btnclick" android:text= "Start" android:layout_height= "Wrap_content"/> <button android:layout_width= "
Match_parent "android:text=" "Stop" android:onclick= "Stopclick" android:layout_height= "wrap_content"/> <View Android:layout_width= "Match_parent" android:layout_height= "1DP" android:background= "#959393" Android:layout_ Marginbottom= "20DP" android:layout_margintop= "20DP"/> <textview android:layout_width= "Match_parent""Android:layout_height=" wrap_content "android:text=" 00:00:00 "android:gravity=" center "android:id=" @+id/text "/ > <button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "Start" Android
: onclick= "Startclick"/> <button android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "Stop" android:onclick= "StopClick1"/> </LinearLayout>
Simple run with a timer to achieve the effect:
Presumably everyone will have their own understanding of this, the official Android Chronometer Way is just to make a timer, and we use our own timer and timertask way to be more autonomous, because you can start from when the time to start from when the timing, Timing way to the countdown is not difficult, even a variety of pompous two seconds, every three seconds, every n seconds is OK, the specific use depends on your choice ~ ~
The above is a small set to introduce the Android time second timer two ways to achieve, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!