如何在 HH:MM:SS 中显示倒数计时器(小时:分钟:秒)
How to Show Countdown timer in HH:MM:SS (Hours : Minutes : Seconds)
如何从我做的XMlandroid开始倒计时'HH: mm: ss',24小时内没有交互就不能再交易或者不能再访问了
此代码是 UI 倒计时器设计的。
倒计时
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constrainlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/bg_color_orange"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/app_bar">
<TextView
android:id="@+id/textView_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="Please make a payment Immediatelly"
android:textColor="@android:color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/constrainlayout" />
<TextView
android:id="@+id/textView_TimeCountDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="13 Hour: 59 Minute: 21 Second"
android:textColor="@android:color/black"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="@+id/textView_Title"
app:layout_constraintStart_toStartOf="@+id/textView_Title"
app:layout_constraintTop_toBottomOf="@+id/textView_Title" />
<TextView
android:id="@+id/textView_Day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginBottom="18dp"
android:text="(Before Thursday, 16 January 2020, 13:33 WIB)"
android:textColor="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/textView_Title"
app:layout_constraintStart_toStartOf="@+id/textView_Title"
app:layout_constraintTop_toBottomOf="@id/textView_TimeCountDown" />
</androidx.constraintlayout.widget.ConstraintLayout>
该代码将像这样显示
对你有帮助
private void startTimer(int noOfMinutes) {
CountDownTimer countDownTimer = new CountDownTimer(noOfMinutes, 1000) {
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
//Convert milliseconds into hour,minute and seconds
String hms = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
countdownTimerText.setText(hms);//set text
}
public void onFinish() {
countdownTimerText.setText("TIME'S UP!!"); //On finish change timer text
}
}.start();
已编辑
//1584700200 is timestamp in milii seconds (Friday, March 20, 2020 10:30:00 AM)
//1000(1sec) is time interval to call onTick method
new CountDownTimer(1584700200, 1000){
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d:%02d",
TimeUnit.HOURS.toDays(TimeUnit.MILLISECONDS.toHours(millis)),
(TimeUnit.MILLISECONDS.toHours(millis) -
TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millis))),
(TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))), (TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
countdownTimerText.setText(hms);//set text
}
@Override
public void onFinish() {
/*clearing all fields and displaying countdown finished message */
countdownTimerText.setText("Count down completed");
}
}.start();
}
Kotlin
private fun countDown() {
val countDownTimer = object : CountDownTimer(1584700200, 1000) {
override fun onTick(p0: Long) {
val millis: Long = p0
val hms = String.format(
"%02d:%02d:%02d:%02d",
TimeUnit.HOURS.toDays(TimeUnit.MILLISECONDS.toDays(millis)),
(TimeUnit.MILLISECONDS.toHours(millis) - TimeUnit.DAYS.toHours(
TimeUnit.MILLISECONDS.toDays(
millis
)
)),
(TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))),
(TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millis)
))
)
System.out.println("Time : " + hms)
countdownTimerText.setText(hms);//set text
}
override fun onFinish() {
/*clearing all fields and displaying countdown finished message */
countdownTimerText.setText("Count down completed");
System.out.println("Time up")
}
}
countDownTimer.start()
}
试试这个代码:
timer = new CountDownTimer(date2, 1000) {
@Override
public void onTick(long l) {
Calendar cal1 = Calendar.getInstance();
long diff = date2 - cal1.getTimeInMillis();
long days = diff / (24 * 60 * 60 * 1000);
int weeks = (int) (days / 7);
long day = days - (weeks * 7);
diff = diff - (days * (24 * 60 * 60 * 1000));
long hours = diff / (60 * 60 * 1000);
diff = diff - (hours * (60 * 60 * 1000));
long minutes = diff / (60 * 1000);
diff = diff - (minutes * (60 * 1000));
long seconds = diff / 1000;
// Log.d(CommonVariables.TAG, "getCounter: " + day + " : " + hours + " : " + minutes + " : " + seconds);
tv_week.setText(weeks + "");
tv_sec.setText(seconds + "");
tv_min.setText(minutes + "");
tv_hours.setText(hours + "");
tv_days.setText(day + "");
}
@Override
public void onFinish() {
}
}.start();
快乐编码
如何从我做的XMlandroid开始倒计时'HH: mm: ss',24小时内没有交互就不能再交易或者不能再访问了
此代码是 UI 倒计时器设计的。
倒计时
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constrainlayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="18dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/bg_color_orange"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/app_bar">
<TextView
android:id="@+id/textView_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="Please make a payment Immediatelly"
android:textColor="@android:color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/constrainlayout" />
<TextView
android:id="@+id/textView_TimeCountDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:text="13 Hour: 59 Minute: 21 Second"
android:textColor="@android:color/black"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="@+id/textView_Title"
app:layout_constraintStart_toStartOf="@+id/textView_Title"
app:layout_constraintTop_toBottomOf="@+id/textView_Title" />
<TextView
android:id="@+id/textView_Day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginBottom="18dp"
android:text="(Before Thursday, 16 January 2020, 13:33 WIB)"
android:textColor="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/textView_Title"
app:layout_constraintStart_toStartOf="@+id/textView_Title"
app:layout_constraintTop_toBottomOf="@id/textView_TimeCountDown" />
</androidx.constraintlayout.widget.ConstraintLayout>
该代码将像这样显示
对你有帮助
private void startTimer(int noOfMinutes) {
CountDownTimer countDownTimer = new CountDownTimer(noOfMinutes, 1000) {
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
//Convert milliseconds into hour,minute and seconds
String hms = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
countdownTimerText.setText(hms);//set text
}
public void onFinish() {
countdownTimerText.setText("TIME'S UP!!"); //On finish change timer text
}
}.start();
已编辑
//1584700200 is timestamp in milii seconds (Friday, March 20, 2020 10:30:00 AM)
//1000(1sec) is time interval to call onTick method
new CountDownTimer(1584700200, 1000){
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = String.format("%02d:%02d:%02d:%02d",
TimeUnit.HOURS.toDays(TimeUnit.MILLISECONDS.toHours(millis)),
(TimeUnit.MILLISECONDS.toHours(millis) -
TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millis))),
(TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))), (TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
countdownTimerText.setText(hms);//set text
}
@Override
public void onFinish() {
/*clearing all fields and displaying countdown finished message */
countdownTimerText.setText("Count down completed");
}
}.start();
}
Kotlin
private fun countDown() {
val countDownTimer = object : CountDownTimer(1584700200, 1000) {
override fun onTick(p0: Long) {
val millis: Long = p0
val hms = String.format(
"%02d:%02d:%02d:%02d",
TimeUnit.HOURS.toDays(TimeUnit.MILLISECONDS.toDays(millis)),
(TimeUnit.MILLISECONDS.toHours(millis) - TimeUnit.DAYS.toHours(
TimeUnit.MILLISECONDS.toDays(
millis
)
)),
(TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))),
(TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millis)
))
)
System.out.println("Time : " + hms)
countdownTimerText.setText(hms);//set text
}
override fun onFinish() {
/*clearing all fields and displaying countdown finished message */
countdownTimerText.setText("Count down completed");
System.out.println("Time up")
}
}
countDownTimer.start()
}
试试这个代码:
timer = new CountDownTimer(date2, 1000) {
@Override
public void onTick(long l) {
Calendar cal1 = Calendar.getInstance();
long diff = date2 - cal1.getTimeInMillis();
long days = diff / (24 * 60 * 60 * 1000);
int weeks = (int) (days / 7);
long day = days - (weeks * 7);
diff = diff - (days * (24 * 60 * 60 * 1000));
long hours = diff / (60 * 60 * 1000);
diff = diff - (hours * (60 * 60 * 1000));
long minutes = diff / (60 * 1000);
diff = diff - (minutes * (60 * 1000));
long seconds = diff / 1000;
// Log.d(CommonVariables.TAG, "getCounter: " + day + " : " + hours + " : " + minutes + " : " + seconds);
tv_week.setText(weeks + "");
tv_sec.setText(seconds + "");
tv_min.setText(minutes + "");
tv_hours.setText(hours + "");
tv_days.setText(day + "");
}
@Override
public void onFinish() {
}
}.start();
快乐编码