[Android Development] use of analogclock and digitalclock

Source: Internet
Author: User

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 );
Super. handlemessage (MSG );
// Continuously obtain the system time through the process

Class looperthread extends thread
Public void run ()
Super. Run ();
/* 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"
Android: Id = "@ + ID/myanalogclock"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
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"
Android: Id = "@ + ID/digitalclock1"
Android: text = "digitalclock"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.