显示计数计时器
Showing the countup timer
我想在我的 android 应用程序中以 yy-MM-dd h:m:s
格式显示倒数计时器
我试过的:
我尝试操纵 CountDownTimer,使其以相反的顺序工作。将间隔更改为 -1000 或每秒向倒计时添加 2000 毫秒。两者都不起作用。
然后我想我应该使用 Chronometer class。就我而言,标准 Chronometer 仅显示小时、分钟和秒。所以没有天,没有月,没有年。
还有一件事
如果时间少于 1 分钟,则计时器仅显示秒数,如下所示
00 秒
如果少于 1 小时则只有分钟和秒那么格式应该是
00 分 00 秒
如果少于 1 天则只有小时、分钟和秒
00 小时 00 分 00 秒
如果少于 1 个月则只显示天、小时、分钟、秒
0 天 00 小时 00 分 00 秒
如果少于一年则只显示月、日、小时、分钟、秒
0 个月 0 天 00 小时 00 分 00 秒
如果超过一年就显示像
0 年 0 个月 0 天 00 小时 00 分 00 秒
我写的代码
chronometer.setFormat("%s");
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
String cTextSize = chronometer.getText().toString();
if ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 60000) {
chronometer.setText(cTextSize.substring(3,5)+" sec");
}
else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 60000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 3600000)){
chronometer.setText(cTextSize.substring(0,2)+" mins "+cTextSize.substring(3,5)+" sec");
}
else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 3600000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 86400000)){
chronometer.setText(cTextSize.substring(0,2)+" hours "+cTextSize.substring(3,5)+" mins "+cTextSize.substring(6,8)+" sec");
}
}
这里的代码运行正常,直到显示小时数,但现在我想如果小时数大于 24,它显示 1 天而不是第 25 小时
非常感谢您的帮助,如果您不知道答案,请不要投反对票
非常感谢所有 google 认证的开发人员帮助我。无论如何,这就是我解决问题的方法
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
String cTextSize = chronometer.getText().toString();
if ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 60000) {
chronometer.setText(cTextSize.substring(3, 5) + " sec");
}
else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 60000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 3600000)) {
chronometer.setText(cTextSize.substring(0, 2) + " mins " + cTextSize.substring(3, 5) + " sec");
}
else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 3600000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 86400000)) {
if (chronometer.getText().length() == 8){
chronometer.setText(cTextSize.substring(0, 2) + " hours " + cTextSize.substring(3, 5) + " mins " + cTextSize.substring(6, 8) + " sec");
}
}
else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 86400000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 2592000000L))
{
Toast.makeText(getActivity(), "day", Toast.LENGTH_SHORT).show();
long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
if(dy==1) {
chronometer.setText(dy + " day " + hr + " hours " + min + " mins " + sec + " sec ");
}else if(dy>1) {
chronometer.setText(dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
}
}
else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 2592000000L) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 31556952000L))
{
Toast.makeText(getActivity(), "month", Toast.LENGTH_SHORT).show();
long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
final long mn = dy / 30;
dy %= 30;
final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
chronometer.setText(mn+" months "+dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
}
else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 31556952000L))
{
Toast.makeText(getActivity(), "year", Toast.LENGTH_SHORT).show();
long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
final long yr = dy / 365;
dy %= 365;
final long mn = dy / 30;
dy %= 30;
final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
chronometer.setText(yr+" years "+mn+" months "+dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
}
}
});
我想在我的 android 应用程序中以 yy-MM-dd h:m:s
格式显示倒数计时器
我试过的: 我尝试操纵 CountDownTimer,使其以相反的顺序工作。将间隔更改为 -1000 或每秒向倒计时添加 2000 毫秒。两者都不起作用。 然后我想我应该使用 Chronometer class。就我而言,标准 Chronometer 仅显示小时、分钟和秒。所以没有天,没有月,没有年。
还有一件事如果时间少于 1 分钟,则计时器仅显示秒数,如下所示
00 秒
如果少于 1 小时则只有分钟和秒那么格式应该是
00 分 00 秒
如果少于 1 天则只有小时、分钟和秒
00 小时 00 分 00 秒
如果少于 1 个月则只显示天、小时、分钟、秒
0 天 00 小时 00 分 00 秒
如果少于一年则只显示月、日、小时、分钟、秒
0 个月 0 天 00 小时 00 分 00 秒
如果超过一年就显示像
0 年 0 个月 0 天 00 小时 00 分 00 秒
我写的代码
chronometer.setFormat("%s");
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
String cTextSize = chronometer.getText().toString();
if ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 60000) {
chronometer.setText(cTextSize.substring(3,5)+" sec");
}
else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 60000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 3600000)){
chronometer.setText(cTextSize.substring(0,2)+" mins "+cTextSize.substring(3,5)+" sec");
}
else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 3600000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 86400000)){
chronometer.setText(cTextSize.substring(0,2)+" hours "+cTextSize.substring(3,5)+" mins "+cTextSize.substring(6,8)+" sec");
}
}
这里的代码运行正常,直到显示小时数,但现在我想如果小时数大于 24,它显示 1 天而不是第 25 小时
非常感谢您的帮助,如果您不知道答案,请不要投反对票
非常感谢所有 google 认证的开发人员帮助我。无论如何,这就是我解决问题的方法
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
String cTextSize = chronometer.getText().toString();
if ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 60000) {
chronometer.setText(cTextSize.substring(3, 5) + " sec");
}
else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 60000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 3600000)) {
chronometer.setText(cTextSize.substring(0, 2) + " mins " + cTextSize.substring(3, 5) + " sec");
}
else if (((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 3600000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 86400000)) {
if (chronometer.getText().length() == 8){
chronometer.setText(cTextSize.substring(0, 2) + " hours " + cTextSize.substring(3, 5) + " mins " + cTextSize.substring(6, 8) + " sec");
}
}
else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 86400000) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 2592000000L))
{
Toast.makeText(getActivity(), "day", Toast.LENGTH_SHORT).show();
long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
if(dy==1) {
chronometer.setText(dy + " day " + hr + " hours " + min + " mins " + sec + " sec ");
}else if(dy>1) {
chronometer.setText(dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
}
}
else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 2592000000L) && ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 31556952000L))
{
Toast.makeText(getActivity(), "month", Toast.LENGTH_SHORT).show();
long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
final long mn = dy / 30;
dy %= 30;
final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
chronometer.setText(mn+" months "+dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
}
else if(((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 31556952000L))
{
Toast.makeText(getActivity(), "year", Toast.LENGTH_SHORT).show();
long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
final long yr = dy / 365;
dy %= 365;
final long mn = dy / 30;
dy %= 30;
final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
chronometer.setText(yr+" years "+mn+" months "+dy + " days " + hr + " hours " + min + " mins " + sec + " sec ");
}
}
});