Digitalclock can be used directly in layout without any code.
Let's take a look at the combination of analogclock and textview (used to simulate digitalclock.
Package com. example;
Import java. util. calendar;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. widget. analogclock;
Import Android. widget. textview;
Public class ex0414 extends activity {
/** Called when the activity is first created .*/
// Declare a constant as the discriminant Information
Protected static final int guinotifier = 0x1234;
// Declare two widget object variables
Private textview mtextview;
Public analogclock manalogclock;
// Declare time-related variables
Public calendar mcalendar;
Public int mMinutes;
Public int mhour;
// Declare the key handler and thread Variables
Public handler mhandler;
Private thread mclockthread;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mtextview = (textview) findviewbyid (R. Id. mytextview );
Manalogclock = (analogclock) findviewbyid (R. Id. myanalogclock );
// Use handler to receive information transmitted by the process and update textview
Mclockthread = new looperthread ();
Mclockthread. Start ();
Mhandler = new handler ()
{
Public void handlemessage (Message MSG)
{
/* Information Processing Method */
Switch (msg. What)
{
Case ex0414.guinotifier:
/* Process the show time event of the textview object here */
Mtextview. settext (mhour + ":" + mMinutes );
Break;
}
Super. handlemessage (MSG );
}
};
// Continuously obtain the system time through the process
}
Class looperthread extends thread
{
Public void run ()
{
Super. Run ();
Try
{
Do
{
/* Obtain the system time */
Long time = system. currenttimemillis ();
Final calendar mcalendar = calendar. getinstance ();
Mcalendar. settimeinmillis (time );
Mhour = mcalendar. Get (calendar. hour );
MMinutes = mcalendar. Get (calendar. Minute );
Thread. Sleep (1000 );
Message M = new message ();
M. What = ex0414.guinotifier;
Ex0414.this. mhandler. sendmessage (m );
} While (ex0414.looperthread. interrupted () = false );
}
Catch (exception E)
{
E. printstacktrace ();
}
}
}
}
The content of Main. XML is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Analogclock
Android: Id = "@ + ID/myanalogclock"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
>
</Analogclock>
<Textview
Android: Id = "@ + ID/mytextview"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "textview"
Android: textsize = "20sp"
Android: layout_gravity = "center_horizontal"
>
</Textview>
<Digitalclock
Android: Id = "@ + ID/digitalclock1"
Android: text = "digitalclock"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
>
</Digitalclock>
</Linearlayout>