我有两个XML的edittext。在一个EditText中,用户可以将一个数字作为分钟,在另一个EditText中,将一个数字作为秒。单击finish按钮后,秒EditText应该开始倒计时,并每秒钟更新一次文本。
此外,我如何才能保持它的更新,直到它达到零分零秒?
我有两个XML的edittext。在一个EditText中,用户可以将一个数字作为分钟,在另一个EditText中,将一个数字作为秒。单击finish按钮后,秒EditText应该开始倒计时,并每秒钟更新一次文本。
此外,我如何才能保持它的更新,直到它达到零分零秒?
当前回答
如果您使用以下代码(如已接受的答案所述),
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
如果不仔细清理引用,将导致使用此代码的活动实例的内存泄漏。
使用以下代码
//Declare timer
CountDownTimer cTimer = null;
//start timer function
void startTimer() {
cTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
}
};
cTimer.start();
}
//cancel timer
void cancelTimer() {
if(cTimer!=null)
cTimer.cancel();
}
你需要调用cTtimer.cancel()每当onDestroy()/onDestroyView()在拥有的活动/片段被调用。
其他回答
输出:01:30
new CountDownTimer(90000, 1000) {
public void onTick(long duration) {
//tTimer.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext resource id
// Duration
long Mmin = (duration / 1000) / 60;
long Ssec = (duration / 1000) % 60;
if (Ssec < 10) {
tTimer.setText("" + Mmin + ":0" + Ssec);
} else tTimer.setText("" + Mmin + ":" + Ssec);
}
public void onFinish() {
tTimer.setText("00:00");
}
}.start();
只需复制粘贴以下代码........
MainActivity
包com.example.countdowntimer;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView text1;
private static final String FORMAT = "%02d:%02d:%02d";
int seconds , minutes;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(TextView)findViewById(R.id.textView1);
new CountDownTimer(16069000, 1000) { // adjust the milli seconds here
public void onTick(long millisUntilFinished) {
text1.setText(""+String.format(FORMAT,
TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
text1.setText("done!");
}
}.start();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="58dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
有Rx的纯溶液,有人可能会感兴趣
数字扩展(Double, Float, Long等):
fun Number.countDownTimer(tick: Long = 1, timeUnit: TimeUnit = TimeUnit.SECONDS): Observable<Long> {
val count = this.toLong()
return Observable.interval(tick, timeUnit)
.take(count)
.map { count - it - 1 }
}
使用
60.countDownTimer().subscribe { textView.text = it }
使用芬兰湾的科特林:
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()
下面是我在Kotlin中使用的解决方案
private fun startTimer()
{
Log.d(TAG, ":startTimer: timeString = '$timeString'")
object : CountDownTimer(TASK_SWITCH_TIMER, 250)
{
override fun onTick(millisUntilFinished: Long)
{
val secondsUntilFinished : Long =
Math.ceil(millisUntilFinished.toDouble()/1000).toLong()
val timeString = "${TimeUnit.SECONDS.toMinutes(secondsUntilFinished)}:" +
"%02d".format(TimeUnit.SECONDS.toSeconds(secondsUntilFinished))
Log.d(TAG, ":startTimer::CountDownTimer:millisUntilFinished = $ttlseconds")
Log.d(TAG, ":startTimer::CountDownTimer:millisUntilFinished = $millisUntilFinished")
}
@SuppressLint("SetTextI18n")
override fun onFinish()
{
timerTxtVw.text = "0:00"
gameStartEndVisibility(true)
}
}.start()
}