Android Chronometer 无法启动
我有一个视图,我想在上面添加一个计时器。然后,在绘制时我想显示当前时间。我是这样做的:
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 但它不会改变。我一定是做错了什么。有什么想法吗?
I have a view and I want to add a chronometer onto it. Then, on draw I want to display the current time. Here is how I did:
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);
}
The initial text 00:00:00 gets drawn but it doesn't change. I must be doing something wrong. Any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经创建了一个局部变量 String currentTime 并且您正在写入该变量而不是全局变量。
You've made a local variable String currentTime and you are writing to that instead of your global variable.