LoginSignup
0
0

Kotlin : Timer 処理を Handler で行う

Posted at

こちらと同じことを行いました。
タイマー処理=一定周期で処理を行う、てのをkotlinで極力カンタンに書いてみる(Handler利用)

プログラム

MainActivity.kt
// ------------------------------------------------------------------
//	timer02
//
//						Oct/25/2023
// ------------------------------------------------------------------
package com.example.timer02

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    var count = 0
    val hnd0 = Handler(Looper.getMainLooper())
    var textMessage: TextView? = null

    val rnb0 = object : Runnable {
        override fun run() {
            count++
            textMessage?.text = count.toString()
            if (count < 50) {
                hnd0.postDelayed(this, 2000)
                println(count)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textMessage = findViewById(R.id.textView)
        hnd0.post(rnb0)
    }
}

// ------------------------------------------------------------------

実行結果

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0