[Android1.5] custom date output format of digitalclock

Source: Internet
Author: User
Tags xml attribute

Preface

Keyword: digitalclock Date Format

The digitalclock of Android does not set the attribute or method of the output format, but it can be implemented through inheritance and rewriting. For details, see the code in the body.

 

Body

I. Requirements

Modify the time output format to show only hours and minutes.

 

II,

    

 

Iii. Description

By looking at the source code, you only need to modify the following two lines of code and copy them all:

Private Final Static string M12 = "H: mm: ss aa ";
Private Final Static string M24 = "K: mm: SS ";

If you want to do better and better, you can expose the method for setting the date format, or add an XML attribute to it.

 

Iv. complete code

Package com. test;

Import java. util. calendar;

Import Android. content. context;
Import Android. content. res. Resources;
Import Android. database. contentobserver;
Import Android. OS. Handler;
Import Android. OS. systemclock;
Import Android. provider. settings;
Import Android. Text. format. dateformat;
Import Android. util. attributeset;

/**
* Customize the digitalclock output format
* @ Author: farmer's uncle
*
*/
Public class digitalclock extends Android. widget. digitalclock {

Calendar mcalendar;
Private Final Static string M12 = "H: mm AA"; // H: mm: SS AA
Private Final Static string M24 = "K: mm"; // K: mm: SS
Private formatchangeobserver mformatchangeobserver;

Private runnable mticker;
Private handler mhandler;

Private Boolean mtickerstopped = false;

String mformat;

Public digitalclock (context ){
Super (context );
Initclock (context );
}

Public digitalclock (context, attributeset attrs ){
Super (context, attrs );
Initclock (context );
}

Private void initclock (context ){
Resources r = context. getresources ();

If (mcalendar = NULL ){
Mcalendar = calendar. getinstance ();
}

Mformatchangeobserver = new formatchangeobserver ();
Getcontext (). getcontentresolver (). registercontentobserver (
Settings. system. content_uri, true, mformatchangeobserver );

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. Text. format. dateformat. is24hourformat (getcontext ());
}

Private void setformat (){
If (get24hourmode ()){
Mformat = M24;
} Else {
Mformat = m12;
}
}

Private class formatchangeobserver extends contentobserver {
Public formatchangeobserver (){
Super (new handler ());
}

@ Override
Public void onchange (Boolean selfchange ){
Setformat ();
}
}
}

5. Usage

<Com. Test. digitalclock Android: layout_x = "15dp" Android: layout_y = "30dp"
Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"
Android: textcolor = "@ Android: color/White" Android: textsize = "55sp"
Android: shadowcolor = "@ Android: color/White" Android: shadowradius = "2.0"
>
</COM. Test. digitalclock>

Code Description:

For Android: shadowcolor, see the translation of my Chinese API documentation.

 

End

Solve the problem.

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.