1. AnalogClock模拟时钟
属性配置
dial
,表盘背景hand_hour
,时针图片hand_minute
,秒针图片
配置文件
<AnalogClock
android:layout_width="100dp"
android:layout_height="100dp" />
<AnalogClock
android:layout_width="150dp"
android:layout_height="150dp"
android:dial="@drawable/analog_clock_dial"
android:hand_minute="@drawable/analog_clock_minute"
android:hand_hour="@drawable/analog_clock_hour" />
效果如下
AnalogClock
添加了对时间事件的监听
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!mAttached) {
mAttached = true;
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
// OK, this is gross but needed. This class is supported by the
// remote views machanism and as a part of that the remote views
// can be inflated by a context for another user without the app
// having interact users permission - just for loading resources.
// For exmaple, when adding widgets from a user profile to the
// home screen. Therefore, we register the receiver as the current
// user not the one the context is for.
getContext().registerReceiverAsUser(mIntentReceiver,
android.os.Process.myUserHandle(), filter, null, getHandler());
}
// NOTE: It's safe to do these after registering the receiver since the receiver always runs
// in the main thread, therefore the receiver can't run before this method returns.
// The time zone may have changed while the receiver wasn't registered, so update the Time
mCalendar = new Time();
// Make sure we update to the current time
onTimeChanged();
}
private void onTimeChanged() {
mCalendar.setToNow();
int hour = mCalendar.hour;
int minute = mCalendar.minute;
int second = mCalendar.second;
mMinutes = minute + second / 60.0f;
mHour = hour + mMinutes / 60.0f;
mChanged = true;
updateContentDescription(mCalendar);
}
2. DigitalClock数字时钟
DigitalClock
显示默认的时间格式,如果想修改,可以修改mFormat
属性
DigitalClock clock = findViewById(R.id.digital_clock);
Class cls = clock.getClass();
try {
Field f = cls.getDeclaredField("mFormat");
f.setAccessible(true);
f.set(clock, "yyyy-MM-dd hh:mm");
} catch (Exception e) {
}
效果如下
DigitalClock
使用线程修改时间
@Override
protected void onAttachedToWindow() {
mTickerStopped = false;
super.onAttachedToWindow();
mFormatChangeObserver = new FormatChangeObserver();
getContext().getContentResolver().registerContentObserver(
Settings.System.CONTENT_URI, true, mFormatChangeObserver);
setFormat();
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();
}
3. TextClock数字时钟
属性配置
format12Hour
,12时制格式format24Hour
,24时制格式
DigitalClock
继承TextView
类,可以设置textSize
和textColor
等属性。并且根据系统配置显示时间格式。
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:format12Hour="yyyy-MM-dd hh:mm aa"
android:format24Hour="yyyy-MM-dd HH:mm"
android:textSize="15sp"
android:textColor="@color/red"/>
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format12Hour="yyyy-MM-dd hh:mm aa"
android:format24Hour="yyyy-MM-dd HH:mm"
android:textSize="12sp"
android:textColor="@color/blue"/>
24进制效果如下
12进制效果如下
TextClock
同样添加了对时间事件的监听
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!mRegistered) {
mRegistered = true;
registerReceiver();
registerObserver();
createTime(mTimeZone);
}
}
private void registerReceiver() {
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
// OK, this is gross but needed. This class is supported by the
// remote views mechanism and as a part of that the remote views
// can be inflated by a context for another user without the app
// having interact users permission - just for loading resources.
// For example, when adding widgets from a managed profile to the
// home screen. Therefore, we register the receiver as the user
// the app is running as not the one the context is for.
getContext().registerReceiverAsUser(mIntentReceiver, android.os.Process.myUserHandle(),
filter, null, getHandler());
}
相关文章
Android Picker控件
Android Clock控件
Android Chronometer控件
Android SystemClock类
Java Calendar类
Java SimpleDateFormat类