我有两个XML的edittext。在一个EditText中,用户可以将一个数字作为分钟,在另一个EditText中,将一个数字作为秒。单击finish按钮后,秒EditText应该开始倒计时,并每秒钟更新一次文本。
此外,我如何才能保持它的更新,直到它达到零分零秒?
我有两个XML的edittext。在一个EditText中,用户可以将一个数字作为分钟,在另一个EditText中,将一个数字作为秒。单击finish按钮后,秒EditText应该开始倒计时,并每秒钟更新一次文本。
此外,我如何才能保持它的更新,直到它达到零分零秒?
当前回答
var futureMinDate = Date()
val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)
try {
futureMinDate = sdf.parse("2019-08-22")
} catch (e: ParseException) {
e.printStackTrace()
}
// Here futureMinDate.time Returns the number of milliseconds since January 1, 1970, 00:00:00 GM
// So we need to subtract the millis from current millis to get actual millis
object : CountDownTimer(futureMinDate.time - System.currentTimeMillis(), 1000) {
override fun onTick(millisUntilFinished: Long) {
val sec = (millisUntilFinished / 1000) % 60
val min = (millisUntilFinished / (1000 * 60)) % 60
val hr = (millisUntilFinished / (1000 * 60 * 60)) % 24
val day = ((millisUntilFinished / (1000 * 60 * 60)) / 24).toInt()
val formattedTimeStr = if (day > 1) "$day days $hr : $min : $sec"
else "$day day $hr : $min : $sec"
tvFlashDealCountDownTime.text = formattedTimeStr
}
override fun onFinish() {
tvFlashDealCountDownTime.text = "Done!"
}
}.start()
传递一个未来日期并将其转换为毫秒。
其他回答
public class Scan extends AppCompatActivity {
int minute;
long min;
TextView tv_timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan2);
tv_timer=findViewById(R.id.tv_timer);
minute=Integer.parseInt("Your time in string form like 10");
min= minute*60*1000;
counter(min);
}
private void counter(long min) {
CountDownTimer timer = new CountDownTimer(min, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
int hours = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
tv_timer.setText(String.format("%d:%d:%d", hours, minutes, seconds));
}
public void onFinish() {
Toast.makeText(getApplicationContext(), "Your time has been completed",
Toast.LENGTH_LONG).show();
}
};
timer.start();
}
}
var futureMinDate = Date()
val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)
try {
futureMinDate = sdf.parse("2019-08-22")
} catch (e: ParseException) {
e.printStackTrace()
}
// Here futureMinDate.time Returns the number of milliseconds since January 1, 1970, 00:00:00 GM
// So we need to subtract the millis from current millis to get actual millis
object : CountDownTimer(futureMinDate.time - System.currentTimeMillis(), 1000) {
override fun onTick(millisUntilFinished: Long) {
val sec = (millisUntilFinished / 1000) % 60
val min = (millisUntilFinished / (1000 * 60)) % 60
val hr = (millisUntilFinished / (1000 * 60 * 60)) % 24
val day = ((millisUntilFinished / (1000 * 60 * 60)) / 24).toInt()
val formattedTimeStr = if (day > 1) "$day days $hr : $min : $sec"
else "$day day $hr : $min : $sec"
tvFlashDealCountDownTime.text = formattedTimeStr
}
override fun onFinish() {
tvFlashDealCountDownTime.text = "Done!"
}
}.start()
传递一个未来日期并将其转换为毫秒。
接口方式。
import android.os.CountDownTimer;
/**
* Created by saikiran on 07-03-2016.
*/
public class CountDownTimerCustom extends CountDownTimer {
private TimeTickListener mTickListener;
private TimeFinishListener mFinishListener;
private long millisUntilFinished;
public CountDownTimerCustom(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void updateTickAndFinishListener(TimeTickListener tickListener) {
mTickListener = tickListener;
}
public void updateFinishListner(TimeFinishListener listener) {
mFinishListener = listener;
}
public long getCurrentMs() {
return millisUntilFinished;
}
public int getCurrentSec() {
return (int) millisUntilFinished / 1000;
}
@Override
public void onTick(long millisUntilFinished) {
this.millisUntilFinished = millisUntilFinished;
if (mTickListener != null)
mTickListener.onTick(millisUntilFinished);
}
@Override
public void onFinish() {
if (mTickListener != null)
mTickListener.onFinished();
mFinishListener.onFinished();
}
public interface TimeTickListener {
void onTick(long mMillisUntilFinished);
void onFinished();
}
public interface TimeFinishListener {
void onFinished();
}
}
// the count down timer
new CountDownTimer(30000, 1000)
{
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
//on finish the count down timer finsih
}
}
}
}.start();
使用芬兰湾的科特林:
var timer = object: CountDownTimer(30000, 1000) {
override fun onTick(millisUntilFinished: Long) {
tvTimer.setText("seconds remaining: " + millisUntilFinished / 1000)
}
override fun onFinish() {
tvTimer.setText("done!")
}
}
timer.start()