通过 CountDownTimer 显示长小数的简单方法 - Android
我有工作代码,但似乎有点奇怪,我必须在 CountDownTimer 上显示小数位,但我还没有找到任何更容易的方法(显然)。
这是我目前拥有的工作代码:
final String tempTitle = TextView.getText().toString();
new CountDownTimer(10000, 100) {
public void onTick(long millisUntilFinished) {
String timeDown = String.valueOf(millisUntilFinished / 100);
String secTime="0";
String sec10th="0";
if (Long.valueOf(millisUntilFinished) < 1000) {
secTime ="0";
sec10th = timeDown.substring(0,1);
}else{
secTime=timeDown.substring(0,1);
sec10th = timeDown.substring(1,2);
}
TextView.setText("Start in: " + secTime + "." + sec10th);
}
public void onFinish() {
TextView.setText(tempTitle);
}
}.start();
}
I have working code but it seems a bit strange that I have to gimmick my way around showing a decimal place on CountDownTimer but I haven't found anything that does it easier (obviously).
Here is the working Code I currently have:
final String tempTitle = TextView.getText().toString();
new CountDownTimer(10000, 100) {
public void onTick(long millisUntilFinished) {
String timeDown = String.valueOf(millisUntilFinished / 100);
String secTime="0";
String sec10th="0";
if (Long.valueOf(millisUntilFinished) < 1000) {
secTime ="0";
sec10th = timeDown.substring(0,1);
}else{
secTime=timeDown.substring(0,1);
sec10th = timeDown.substring(1,2);
}
TextView.setText("Start in: " + secTime + "." + sec10th);
}
public void onFinish() {
TextView.setText(tempTitle);
}
}.start();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除以 100.0,这样就不需要进行整数除法。
根据您的评论进行编辑:
你可以做
或#.###等,具体取决于你想要多少位小数......
divide by 100.0 so you dont do integer division.
Edit per your comment:
you could do
or #.### etc depending on how many decimals you want...