如何在android中做倒数计时器?

我有两个编辑文本在XML中,在一个编辑文本用户可以把一个数字作为分钟,另一个作为秒。 点击完成button后,秒编辑文本应该开始倒计时,并每秒更新其文本。 如何才能保持这种更新,直到零分零秒?

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(); 

参考这个链接 。

只需复制粘贴下面的代码……..

主要活动

包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> 

在这里输入图像描述

MainActivity.java

 package com.zt.countdown; import java.text.SimpleDateFormat; import java.util.Date; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView tvDay, tvHour, tvMinute, tvSecond, tvEvent; private LinearLayout linearLayout1, linearLayout2; private Handler handler; private Runnable runnable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); countDownStart(); } @SuppressLint("SimpleDateFormat") private void initUI() { linearLayout1 = (LinearLayout) findViewById(R.id.ll1); linearLayout2 = (LinearLayout) findViewById(R.id.ll2); tvDay = (TextView) findViewById(R.id.txtTimerDay); tvHour = (TextView) findViewById(R.id.txtTimerHour); tvMinute = (TextView) findViewById(R.id.txtTimerMinute); tvSecond = (TextView) findViewById(R.id.txtTimerSecond); tvEvent = (TextView) findViewById(R.id.tvevent); } // //////////////COUNT DOWN START///////////////////////// public void countDownStart() { handler = new Handler(); runnable = new Runnable() { @Override public void run() { handler.postDelayed(this, 1000); try { SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd"); // Here Set your Event Date Date eventDate = dateFormat.parse("2017-12-30"); Date currentDate = new Date(); if (!currentDate.after(eventDate)) { long diff = eventDate.getTime() - currentDate.getTime(); long days = diff / (24 * 60 * 60 * 1000); diff -= days * (24 * 60 * 60 * 1000); long hours = diff / (60 * 60 * 1000); diff -= hours * (60 * 60 * 1000); long minutes = diff / (60 * 1000); diff -= minutes * (60 * 1000); long seconds = diff / 1000; tvDay.setText("" + String.format("%02d", days)); tvHour.setText("" + String.format("%02d", hours)); tvMinute.setText("" + String.format("%02d", minutes)); tvSecond.setText("" + String.format("%02d", seconds)); } else { linearLayout1.setVisibility(View.VISIBLE); linearLayout2.setVisibility(View.GONE); tvEvent.setText("Android Event Start"); handler.removeCallbacks(runnable); // handler.removeMessages(0); } } catch (Exception e) { e.printStackTrace(); } } }; handler.postDelayed(runnable, 0); } // //////////////COUNT DOWN END///////////////////////// } 

activity_main.xml中

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/ll1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:background="@drawable/counter_back" android:gravity="center" android:orientation="horizontal" android:visibility="gone" > <TextView android:id="@+id/tvevent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal|center_vertical" android:singleLine="true" android:text="Android Event Start" android:textColor="#fff" android:textSize="24sp" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:id="@+id/ll2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:background="@drawable/counter_back" android:gravity="center" android:orientation="horizontal" android:visibility="visible" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/counter_back" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/txtTimerDay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3" android:gravity="center" android:text="00" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#fff" /> <TextView android:id="@+id/txt_TimerDay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="Days" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#fff" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/counter_back" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/txtTimerHour" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3" android:gravity="center" android:text="00" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#fff" /> <TextView android:id="@+id/txt_TimerHour" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="Hour" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#fff" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/counter_back" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/txtTimerMinute" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3" android:gravity="center" android:text="00" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#fff" /> <TextView android:id="@+id/txt_TimerMinute" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="Minute" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#fff" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/counter_back" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/txtTimerSecond" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3" android:gravity="center" android:text="00" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#fff" /> <TextView android:id="@+id/txt_TimerSecond" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:text="Second" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="#fff" /> </LinearLayout> </LinearLayout> </LinearLayout> 

如果您使用下面的代码(如接受的答案中所述)

 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(); } 

只要调用拥有Activity / Fragment的onDestroy()/ onDestroyView() ,就需要调用cTtimer.cancel()

通过传递秒和textview对象来调用下面的函数

 public void reverseTimer(int Seconds,final TextView tv){ new CountDownTimer(Seconds* 1000+1000, 1000) { public void onTick(long millisUntilFinished) { int seconds = (int) (millisUntilFinished / 1000); int minutes = seconds / 60; seconds = seconds % 60; tv.setText("TIME : " + String.format("%02d", minutes) + ":" + String.format("%02d", seconds)); } public void onFinish() { tv.setText("Completed"); } }.start(); } 

Revers CountDown计时器与小时分钟和秒

 public void reverseTimer(int Seconds, final TextView tv) { new CountDownTimer(Seconds * 1000 + 1000, 1000) { public void onTick(long millisUntilFinished) { int seconds = (int) (millisUntilFinished / 1000); int hours = seconds / (60 * 60); int tempMint = (seconds - (hours * 60 * 60)); int minutes = tempMint / 60; seconds = tempMint - (minutes * 60); tv.setText("TIME : " + String.format("%02d", hours) + ":" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds)); } public void onFinish() { tv.setText("Completed"); } }.start(); }