Android 计时器滴答一次后停止
我需要在 10 秒后隐藏 TextView。我想我可以实现一个计时器,当经过的时间大于 10000 毫秒时,我隐藏 TextView。我的问题是计时器只滴答一次然后就停止了。知道我错过了什么吗?
Activity ctx = this;
...
private void ShowText(String message)
{
txtProceed.setText(message);
txtProceed.setVisibility(View.VISIBLE);
chronoHideText = new Chronometer(ctx);
chronoHideText.setOnChronometerTickListener(new OnChronometerTickListener()
{
public void onChronometerTick(Chronometer arg0) {
long elapsed = SystemClock.elapsedRealtime() - chronoHideText.getBase();
Log.i("Chrono",String.valueOf(elapsed));
if (elapsedTime>10000)
{
txtProceed.setVisibility(View.INVISIBLE);
chronoHideText.stop();
}
}
}
);
chronoHideText.setBase(SystemClock.elapsedRealtime());
chronoHideText.start();
}
谢谢贾努斯的帮助。现在效果很好的解决方案是:
Handler splashHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
//remove SplashScreen from view
txtProceed.setVisibility(View.INVISIBLE);
break;
}
super.handleMessage(msg);
}
};
Message msg = new Message();
msg.what = 0;
splashHandler.sendMessageDelayed(msg, 10000);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 handleMessage 方法。
这样你就不需要自己测量时间并每秒或毫秒进行计算工作。在 Android 操作系统传送消息之前,您的应用程序不会占用任何 cpu 时间。
You can use a handler that sends a delayed message.
The method sendMessageDelayed gives you the ability to specify a time and after that time elapsed you will get a message. If you only need to do one thing after the elapsed time you can just hide the view in your handleMessage method.
This way you don't need to measure the time yourself and do computation work every second or millisecond. Your app won't take any cpu time until the message is delivered by the Android OS.
}
}