WEB开发网
开发学院手机开发Android 开发 Android1.5 DigitalClock自定义日期输出格式 阅读

Android1.5 DigitalClock自定义日期输出格式

 2010-08-29 01:47:00 来源:WEB开发网   
核心提示:}private void initClock(Context context) {Resources r = context.getResources();if (mCalendar == null) {mCalendar = Calendar.getInstance();}mFormatChangeObserver

}

private void initClock(Context 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) {

Tags:Android DigitalClock 定义

编辑录入:coldstar [复制链接] [打 印]
赞助商链接