CountDownTimer
![](countdowntimer.gif)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="5"
android:textSize="140sp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout>
package com.example.app24;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
CountDownTimer timer;
TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView)this.findViewById(R.id.textView1);
timer=new CountDownTimer(5000,10) {
@Override
public void onTick(long millisUntilFinished) {
Log.e("MainActivity", "經過時間:"+millisUntilFinished);
textView1.setText(""+millisUntilFinished/1000);
}
@Override
public void onFinish() {
Log.e("MainActivity","時間到");
textView1.setVisibility(View.GONE);
}
};
timer.start();
}
}