Android allows textview to support HTML tags for custom clock display components digitalclock

Source: Internet
Author: User
Android allows textview to support HTML tags for custom clock display components digitalclock

The default digitalclock is displayed in seconds, and the font size in the morning and afternoon is as large as the time. The clock effect is to display only the time, updated every minute, and the font in the morning and afternoon is smaller than the number one.

Use custom digitalclock and textview HTML functions.

<Linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginleft = "28dip" Android: layout_margintop = "5dip" Android: align = "center" Android: gravity = "center" Android: Background = "@ drawable/watch"> <COM. cyberblue. util. mydigitalclockandroid: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textcolor = "@ color/White" Android: layout_gravity = "Center" Android: gravity = "center" Android: textstyle = "bold" Android: textsize = "29dip"/> </linearlayout >======================= ============================== package COM. cyberblue. util; 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. tex T. format. dateformat; import android. util. attributeset; import android. util. log; import android. widget. textview; import Java. util. calendar;/*** like analogclock, but digital. shows seconds. ** fixme: implement separate views for hours/minutes/seconds, so * proportional fonts don't shake rendering */public class mydigitalclock extends textview {calendar mcalendar; private Final Static string M12 = "H: Mm AA "; private final static string M24 =" K: mm "; private formatchangeobserver mformatchangeobserver; private runnable mticker; private handler mhandler; private Boolean mtickerstopped = false; string mformat; public mydigitalclock (context) {super (context); initclock (context);} public mydigitalclock (context, attributeset attrs) {super (context, attrs ); initclock (context);} priv Ate 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; supe R. 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 (); string content = (string) dateformat. format (mformat, mcalendar); If (content. split (""). length> 1) {// The span style method does not work -- cannot parse <font size = \ "1 \" Nor does content = Co Ntent. split ("") [0] + "<small> <font size = \" 1 \ "color = \" # 66ccff \ ">" + content. split ("") [1] + "</font> </small>";} settext (Android. text. HTML. fromhtml (content); invalidate (); Long Now = systemclock. uptimemillis (); // update long next = now + (60*1000-now % 1000) every minute; 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 ();}}} ========================================================== ===================================== Android textview HTML textview = (textview) findviewbyid (R. id. hello); textview. settext (HTML. fromhtml ("Hello <B> world </B>, <font size = \" 3 \ "color = \" Red \ "> analysisxmlactivty! </Font> "); HTML tags supported by Android <a href = "... "> define link content <B> define Bold Text B is the abbreviation of blod <big> define large font text <BLOCKQUOTE> Reference Block label attribute: common -- General attribute cite -- URI of the referenced content <br> defines a line feed <cite> indicates the referenced URI <dfn> defines the tag dfn, which is the abbreviation of the defining instance <Div align = ". .. "> <em> emphasize that tag em is short for emphasis <font size = "... "color = "... "face = "... "> ========================================================== ========

Appendix: how to obtain whether the Android system time is in the 24-hour or 12-hour contentresolver CV = this. getcontentresolver (); string strtimeformat = android. provider. settings. system. getstring (CV, android. provider. settings. system. time_12_24); If (strtimeformat. equals ("24") {log. I ("activity", "24");} ---------------------------------------------------------------------- <? 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: gravity = "center_horizontal"> <! -- Define a simulated clock --> <analogclock Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <! -- Define a digital clock --> <digitalclock Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textsize = "14pt"/>

</Linearlayout>

 

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.