In Android if the digital hour is generally used digitalclock, this class is Google to our developers to provide a convenient class to achieve the function of the digital hour, now write a demo, why do you want to talk about this class? Because I'm going to talk about the ListView. Item Countdown function, so first practice this foundation, and how it is implemented, new Android project Digitalclockdemo
Layout Activity_main.xml File
<relativelayout 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 " > <digitalclock android:id= "@+id/digitalclock" android:layout_width= "Wrap_content" android: layout_height= "Wrap_content" android:layout_centerinparent= "true" android:textsize= "25sp " Android:textcolor= "#ff00ff" /></relativelayout>
The code doesn't have to be written, and it works directly.
This number is a change of this is just a static diagram, why the code is not written will come out of this effect, so you have to look at his source
public class DigitalClock extends TextView {Calendar Mcalendar; Private final static String M12 = "H:mm:ss AA"; Private final static String M24 = "K:mm:ss"; Private Formatchangeobserver Mformatchangeobserver; Private Runnable Mticker; Private Handler Mhandler; Private Boolean mtickerstopped = false; String Mformat; Public DigitalClock (Context context) {super (context); Initclock (context); } public DigitalClock (context context, AttributeSet Attrs) {Super (context, attrs); Initclock (context); } private void Initclock (context context) {Resources R = mcontext.getresources (); if (Mcalendar = = null) {Mcalendar = Calendar.getinstance (); } mformatchangeobserver = new Formatchangeobserver (); GetContext (). Getcontentresolver (). Registercontentobserver (Settings.System.CONTENT_URI, True, Mformatchange Observer); SetFormat (); } @Override protected void OnATtachedtowindow () {mtickerstopped = false; Super.onattachedtowindow (); Mhandler = new Handler (); /** * Requests a tick on the next hard-second boundary */mticker = new Runnable () { public void Run () {if (mtickerstopped) return; Mcalendar.settimeinmillis (System.currenttimemillis ()); SetText (Dateformat.format (Mformat, Mcalendar)); Invalidate (); Long now = Systemclock.uptimemillis (); Long next = Now + (1000-now% 1000); Mhandler.postattime (Mticker, next); } }; Mticker.run (); } @Override protected void Ondetachedfromwindow () {Super.ondetachedfromwindow (); Mtickerstopped = true; }/** * pulls 12/24 mode from system settings */Private Boolean Get24hourmode () {return Android.tex T.format.dateformat.is24hourformaT (GetContext ()); private void SetFormat () {if (Get24hourmode ()) {Mformat = M24; } else {mformat = M12; }} Private class Formatchangeobserver extends Contentobserver {public formatchangeobserver () {s Uper (New Handler ()); } @Override public void OnChange (Boolean selfchange) {SetFormat (); } }}
This kind of source code is not too much, so have patience to read all,
First know that the DigitalClock class is inheriting this TextView
Start looking at the Initclock () method from the constructor, the code is as follows
private void Initclock (context context) {
Resources r = mcontext.getresources ();
if (Mcalendar = = null) {
Mcalendar = Calendar.getinstance ();
}
Mformatchangeobserver = new Formatchangeobserver ();
GetContext (). Getcontentresolver (). Registercontentobserver (
Settings.System.CONTENT_URI, True, mformatchangeobserver);
SetFormat ();
}
The SetFormat () method is displayed by default for 24 hours, the most important being the Onattachedtowindow () method,
Onattachedtowindow () is mounted on the form and will be executed after the activity's Onresume () method,
The key in the Onattachedtowindow () method is this code:
Mhandler = new Handler ();
/**
* Requests a tick on the next hard-second boundary
*/
Mticker = new Runnable () {
public void Run () {
if (mtickerstopped) return;
Mcalendar.settimeinmillis (System.currenttimemillis ());
SetText (Dateformat.format (Mformat, Mcalendar));
Invalidate ();
Long now = Systemclock.uptimemillis ();
Long next = Now + (1000-now% 1000);
Mhandler.postattime (Mticker, next);
}
};
Mticker.run ();
This is the core of the whole class,
Mcalendar.settimeinmillis (System.currenttimemillis ());
SetText (Dateformat.format (Mformat, Mcalendar));
These two lines of code are the time displayed on our desktop DigitalClock has been implemented internally, so why don't we have to write code to see the digital hour time,
Invalidate (); The UI will be redrawn again without time.
Long now = Systemclock.uptimemillis ();// number of milliseconds from boot to current (phone sleep time not included);
Long next = Now + (1000-now% 1000);
Mhandler.postattime (Mticker, next);
Long next = Now + (1000-now% 1000); Does this line of code mean you can't read?
Android from source with you familiar with DigitalClock Digital hour app and its usage scene