<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/timerView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="60sp" /> </LinearLayout>
public class MyChronometer extends Activity {
private TextView timerView;
private long baseTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.chrono);
MyChronometer.this.baseTimer = SystemClock.elapsedRealtime();
timerView = (TextView) this.findViewById(R.id.timerView);
final Handler startTimehandler = new Handler(){
public void handleMessage(android.os.Message msg) {
if (null != timerView) {
timerView.setText((String) msg.obj);
}
}
};
new Timer("开机计时器").scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
int time = (int)((SystemClock.elapsedRealtime() - MyChronometer.this.baseTimer) / 1000);
String hh = new DecimalFormat("00").format(time / 3600);
String mm = new DecimalFormat("00").format(time % 3600 / 60);
String ss = new DecimalFormat("00").format(time % 60);
String timeFormat = new String(hh + ":" + mm + ":" + ss);
Message msg = new Message();
msg.obj = timeFormat;
startTimehandler.sendMessage(msg);
}
}, 0, 1000L);
super.onCreate(savedInstanceState);
}
public class MyChronometer extends Activity {
private TextView timerView;
private long baseTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.chrono);
MyChronometer.this.baseTimer = SystemClock.elapsedRealtime();
timerView = (TextView) this.findViewById(R.id.timerView);
Handler myhandler = new Handler(){
public void handleMessage(android.os.Message msg) {
if (0 == MyChronometer.this.baseTimer) {
MyChronometer.this.baseTimer = SystemClock.elapsedRealtime();
}
int time = (int)((SystemClock.elapsedRealtime() - MyChronometer.this.baseTimer) / 1000);
String hh = new DecimalFormat("00").format(time / 3600);
String mm = new DecimalFormat("00").format(time % 3600 / 60);
String ss = new DecimalFormat("00").format(time % 60);
if (null != MyChronometer.this.timerView) {
timerView.setText(hh + ":" + mm + ":" + ss);
}
sendMessageDelayed(Message.obtain(this, 0x0), 1000);
}
};
myhandler.sendMessageDelayed(Message.obtain(myhandler, 0x0), 1000);
super.onCreate(savedInstanceState);
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="60sp" />
</LinearLayout>
public class MyChronometer extends Activity {
Chronometer chronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.chrono);
chronometer = (Chronometer) this.findViewById(R.id.chronometer);
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
super.onCreate(savedInstanceState);
}
}
public void start() {
mStarted = true;
updateRunning();
}
private void updateRunning() {
boolean running = mVisible && mStarted;
if (running != mRunning) {
if (running) {
updateText(SystemClock.elapsedRealtime());
dispatchChronometerTick();
mHandler.sendMessageDelayed(Message.obtain(mHandler, TICK_WHAT), 1000);
} else {
mHandler.removeMessages(TICK_WHAT);
}
mRunning = running;
}
}
private Handler mHandler = new Handler() {
public void handleMessage(Message m) {
if (mRunning) {
updateText(SystemClock.elapsedRealtime());
dispatchChronometerTick();
sendMessageDelayed(Message.obtain(this, TICK_WHAT), 1000);
}
}
};
private synchronized void updateText(long now) {
long seconds = now - mBase;
seconds /= 1000;
String text = DateUtils.formatElapsedTime(mRecycle, seconds);
if (mFormat != null) {
Locale loc = Locale.getDefault();
if (mFormatter == null || !loc.equals(mFormatterLocale)) {
mFormatterLocale = loc;
mFormatter = new Formatter(mFormatBuilder, loc);
}
mFormatBuilder.setLength(0);
mFormatterArgs[0] = text;
try {
mFormatter.format(mFormat, mFormatterArgs);
text = mFormatBuilder.toString();
} catch (IllegalFormatException ex) {
if (!mLogged) {
Log.w(TAG, "Illegal format string: " + mFormat);
mLogged = true;
}
}
}
setText(text);
}
public static String formatElapsedTime(StringBuilder recycle, long elapsedSeconds) {
Formatter f = new Formatter(sb, Locale.getDefault());
initFormatStrings();
if (hours > 0) {
return f.format(sElapsedFormatHMMSS, hours, minutes, seconds).toString();
} else {
return f.format(sElapsedFormatMMSS, minutes, seconds).toString();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有