我有一个视图,想在其上添加计时器。然后,在绘制时,我想显示当前时间。这是我的做法:
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”被绘制出来,但它没有改变。我一定做错了什么。有什么想法吗?
chrono.start()
和chrono.setBase()
在哪里?是在onCreate()
里面吗? - st0le