Android Studio: CountDownTimer の使い方

参考ページ
[CountDownTimer](https://developer.android.com/reference/kotlin/android/os/CountDownTimer)

## プログラム

```xml:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="60sp"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
```

```kt:MainActivity.kt
package com.example.disp01

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var textMessage = findViewById<TextView>(R.id.text_message)

        object : CountDownTimer(10000, 1000) {

            override fun onTick(millisUntilFinished: Long) {
                var nn = millisUntilFinished / 1000
                textMessage.setText("残り時間: " + nn)
            }

            override fun onFinish() {
                textMessage.setText("完了!")
            }
        }.start()


    }
}
```

## 実行結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/13b34f20-af32-da99-83a3-10be1349b39b.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/d430800b-c819-ad01-79ba-21f102df807c.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/d41f9b5f-35bf-092b-543c-69c21532006f.png)

## 改造したプログラム
曜日を表示してみます。

```kt:MainActivity.kt
package com.example.disp01

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var textMessage = findViewById<TextView>(R.id.text_message)

        val days = arrayOf("日","月","火","水","木","金","土")

        var count = 0

        object : CountDownTimer(14000, 2000) {

            override fun onTick(millisUntilFinished: Long) {
                textMessage.setText(days[count])

                count+=1
                count = count % 7

            }

            override fun onFinish() {
                textMessage.setText("完了!")
            }
        }.start()


    }
}
```

実行した時の様子
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/a4cbaed5-08d9-9d8e-6edc-78a7fa9361e3.png)