我有一个视图,我想在上面添加一个计时器。然后,在绘制时我想显示当前时间。这是我的做法:
public class MyView extends View {
private Chronometer chrono;
private long elapsedTime=0;
private String currentTime="00:00:00";
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}
private void init(Context ctx){
chrono= new Chronometer(ctx);
chrono.setText("Time: 00:00:00");
chrono.setOnChronometerTickListener(new OnChronometerTickListener()
{
public void onChronometerTick(Chronometer arg0) {
String HH =((elapsedTime / 3600) < 10 ? "0" : "") + (elapsedTime / 3600);
String MM =((elapsedTime / 60) < 10 ? "0" : "") + (elapsedTime / 60);
String SS =((elapsedTime % 60) < 10 ? "0" : "") + (elapsedTime % 60);
String currentTime = HH+":"+MM+":"+SS;
elapsedTime = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
arg0.setText(currentTime);
}
}
);
}
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawText(currentTime, someWidth , someHeight, somePaint);
}
初始文本 00:00:00 被绘制但不会改变。我一定做错了什么。有任何想法吗 ?