android wenbview 自定义,Android自定义格式ClockTextView

这篇博客介绍了一种方法,通过扩展Android自带的DigitalClock类并添加setFormat方法,来实现自定义时间显示格式的功能。作者创建了一个名为ClockText的类,该类覆盖了DigitalClock的功能,并能根据传入的格式字符串动态更新时间显示。博客内容包括类的详细实现代码,以及如何使用这个自定义组件。
摘要由CSDN通过智能技术生成

android 自带的DigitalClock 不能自定义时间显示格式。我把代码重写了写了下, 添加了一个公有方法setFormat(String format) 自定义显示格式

1.[代码][Java]代码

初始化程序: ClockText clock = new ClockText(this);

clock.setFormat("yyyy-MM-dd hh:mm:ss");

源码:/**

*

* @author ClassNotFoundException

*

*/

public class ClockText extends TextView{

Calendar mCalendar;

@SuppressWarnings("FieldCanBeLocal") // We must keep a reference to this observer

private FormatChangeObserver mFormatChangeObserver;

private Runnable mTicker;

private Handler mHandler;

private boolean mTickerStopped = false;

private String mFormat;

public ClockText(Context context) {

super(context);

initClock();

}

public ClockText(Context context, AttributeSet attrs) {

super(context, attrs);

initClock();

}

private void initClock() {

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;

}

private void setFormat() {

mFormat = mFormat;

}

public void setFormat(String format){

this.mFormat = format;

}

private class FormatChangeObserver extends ContentObserver {

public FormatChangeObserver() {

super(new Handler());

}

@Override

public void onChange(boolean selfChange) {

setFormat();

}

}

@Override

public void onInitializeAccessibilityEvent(AccessibilityEvent event) {

super.onInitializeAccessibilityEvent(event);

//noinspection deprecation

event.setClassName(ClockText.class.getName());

}

@Override

public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {

super.onInitializeAccessibilityNodeInfo(info);

//noinspection deprecation

info.setClassName(ClockText.class.getName());

}

}

2.[文件] ClockText.java ~ 3KB     下载(22)

package com.nb82.util;

import java.util.Calendar;

import android.content.Context;

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;

import android.view.accessibility.AccessibilityEvent;

import android.view.accessibility.AccessibilityNodeInfo;

import android.widget.TextView;

/**

*

* @author ClassNotFoundException

*

*/

public class ClockText extends TextView{

Calendar mCalendar;

@SuppressWarnings("FieldCanBeLocal") // We must keep a reference to this observer

private FormatChangeObserver mFormatChangeObserver;

private Runnable mTicker;

private Handler mHandler;

private boolean mTickerStopped = false;

private String mFormat;

public ClockText(Context context) {

super(context);

initClock();

}

public ClockText(Context context, AttributeSet attrs) {

super(context, attrs);

initClock();

}

private void initClock() {

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;

}

private void setFormat() {

mFormat = mFormat;

}

public void setFormat(String format){

this.mFormat = format;

}

private class FormatChangeObserver extends ContentObserver {

public FormatChangeObserver() {

super(new Handler());

}

@Override

public void onChange(boolean selfChange) {

setFormat();

}

}

@Override

public void onInitializeAccessibilityEvent(AccessibilityEvent event) {

super.onInitializeAccessibilityEvent(event);

//noinspection deprecation

event.setClassName(ClockText.class.getName());

}

@Override

public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {

super.onInitializeAccessibilityNodeInfo(info);

//noinspection deprecation

info.setClassName(ClockText.class.getName());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值