main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Chronometer
android:id="@+id/myChronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始计时" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止计时" />
<Button
android:id="@+id/btnBase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复位" />
<Button
android:id="@+id/btnFormat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="格式化显示" />
</LinearLayout>

</LinearLayout>


.java代码如下:

package org.lxh.demo;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.TextView;

public class Hello extends Activity {
private Chronometer myChronometer=null;
private Button btnStart=null;
private Button btnStop=null;
private Button btnBase=null;
private Button btnFormat=null;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.main); // 设置要使用的布局管理器
this.myChronometer=(Chronometer)super.findViewById(R.id.myChronometer);
this.btnStart=(Button)super.findViewById(R.id.btnStart);
this.btnStop=(Button)super.findViewById(R.id.btnStop);
this.btnBase=(Button)super.findViewById(R.id.btnBase);
this.btnFormat=(Button)super.findViewById(R.id.btnFormat);
this.btnStart.setOnClickListener(new OnClickListenerStart());
this.btnStop.setOnClickListener(new OnClickListenerStop());
this.btnBase.setOnClickListener(new OnClickListenerBase());
this.btnFormat.setOnClickListener(new OnClickListenerFormat());
}
private class OnClickListenerStart implements OnClickListener{

public void onClick(View arg0) {
Hello.this.myChronometer.start();

}

}
private class OnClickListenerStop implements OnClickListener{

public void onClick(View arg0) {
Hello.this.myChronometer.stop();

}

}
private class OnClickListenerBase implements OnClickListener{

public void onClick(View arg0) {
Hello.this.myChronometer.setBase(SystemClock.elapsedRealtime());

}

}
private class OnClickListenerFormat implements OnClickListener{

public void onClick(View arg0) {
Hello.this.myChronometer.setFormat("新的显示格式:%s。");

}

}

}


运行效果如下:

Chronometer组件实现计时器小程序_bundle