3 回答
TA贡献1712条经验 获得超3个赞
根据要求,这里是一次尝试。请注意,该答案尚未经过测试。
所有与 不相关的数据Time均已被删除。
int timeInMinutes = 1;
countDownTimer = new CountDownTimer(timeInMinutes * 60000, 1000) {
public void onTick(long millisUntilFinished) {
Time time = new Time(millisUntilFinished);
final int hours = time.getHours();
final int min = time.getMinutes();
final int scnds = time.getSeconds();
countdownText.setText("" + scnds);
}
public void onFinish() {
// Everytime the countDown stops restart it?
//countDownTimer.start();
}
}.start();
TA贡献1783条经验 获得超4个赞
我相信这让我走上了正确的轨道,虽然它显然不正确,因为 .getHours 和 .getMinutes 显然自 api 14 以来已经贬值了。我发现我可以进行比较,然后根据时间进行设置
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int timeinminutes = 1;
countDownTimer = new CountDownTimer(timeinminutes * 60000, 1000) {
public void onTick(long millisUntilFinished) {
Time time = new Time(millisUntilFinished);
final int hours = time.compareTo(Calendar.getInstance().getTime()); //added a compareto and set that against what the current time is
final int min = time.getMinutes();
//final int scnds = time.getSeconds();
long scnds = 1;
scnds = (millisUntilFinished / 1000);
countdownText.setText("" + scnds);
countdownHours.setText("" +hours); // sets to ID of textview
}
public void onFinish() {
countDownTimer.start();
}
}.start();
当我测试它时,它说 1 小时(当前时间是 22:54),所以我确定它是正确的,但我必须明天检查。但谢谢你给我指明了正确的方向 :D 我现在需要做的就是弄清楚如何抵消时间,所以目前应该是 4 小时
TA贡献1805条经验 获得超10个赞
从互联网上搜索了很多之后,我无法获得任何内置功能。也许有任何功能,但对我来说,我习惯做这样的代码,它确实对我有用。只需复制粘贴它并根据您的文本视图设置timerTV。
var cT: CountDownTimer? = null
fun timer() {
val calendar = Calendar.getInstance()
calendar.time
Log.d("Current time: ", calendar.time.toString())
val hourCurrent = calendar.get(Calendar.HOUR)
val minutCurrent = calendar.get(Calendar.MINUTE)
val secondCurrent = calendar.get(Calendar.SECOND)
var hourSub = 24 - hourCurrent
var minSub = 60 - minutCurrent
var secSub = 60 - secondCurrent
cT = object : CountDownTimer(1000, 1000) {
override fun onTick(millisUntilFinished: Long) {
timerTV.setText("$hourSub:$minSub:$secSub")
}
override fun onFinish() {
timer()
}
}
cT?.start()
}
override fun onDestroy() {
super.onDestroy()
cT?.cancel()
}
添加回答
举报