AnalogClock继承自View,重写了View组件的OnDraw方法,使其在View上显示模拟时钟。
DigitalClock继承自TextView,所以可以使用TextView的XML属性和方法,其本身就是一个文本框,只不过显示的是系统当前时间。
下面是一个很简单的使用示例,只需修改XML布局文件即可。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Clock" >
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<DigitalClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11pt" />
</LinearLayout>
另外,Android还提供了一个计时器组件,Chronometer。它也继承自TextView,但它显示的是从某段时间开始过去了多长时间。
Chronometer只提供了一个XML属性:android:format,用于指定计时器的计时格式。
Chronometer支持以下常用方法:setBase(long base):设置计时器的起始时间;
setFormat(String format):设置显示时间的格式;
start():开始计时;
stop():停止计时;
setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener):绑定事件监听器,当计时器改变时触发该监听器。
下面是一个简单的计时器示例。
Java源代码:
package com.example.chronometer;
import java.text.ChoiceFormat;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class ChronometerTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chronometer_test);
final Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);
Button start = (Button) findViewById(R.id.start);
Button stop = (Button) findViewById(R.id.stop);
Button reset = (Button) findViewById(R.id.reset);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub9
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
chronometer.stop();
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
chronometer.setBase(SystemClock.elapsedRealtime());
}
});
chronometer.setFormat("计时:%s");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_chronometer_test, menu);
return true;
}
}
XML源代码:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0,1"
tools:context=".ChronometerTest" >
<Chronometer
android:id="@+id/chronometer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<TableRow>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5pt"
android:layout_toRightOf="@id/start"
android:text="Stop" />
</TableRow>
<Button
android:id="@+id/reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/start"
android:text="Reset" />
</TableLayout>