LoginSignup
0
0

AndroidStudio: Handler の使い方

Last updated at Posted at 2023-10-21

こちらと同じことを行いました。
【Android】Handlerによる周期処理 in Kotlin

プロジェクトの作成

プロジェクト名: handler01
Empty Views Activity を選びます。

画面

次を TextView に加えます。

android:id="@+id/text_view"
android:textSize="60sp"
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_view"
        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>

プログラム

MainActivity.kt
package com.example.handler01

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

class MainActivity : AppCompatActivity(), Runnable {

    private var count : Int = 0
    private val handler : Handler = Handler(Looper.getMainLooper())

    //--------------------------------------------------------------------------------------
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d("app7/status", "onCreate")
    }

    //--------------------------------------------------------------------------------------
    private fun countUp() : Int {

        // カウントアップ
        if(count < Integer.MAX_VALUE) {
            count++
        }
        Log.d("app7/countUp(count)", count.toString())

        val textView : TextView = findViewById(R.id.text_view)
        textView.text = count.toString()

        return(count)
    }

    //--------------------------------------------------------------------------------------
    override fun run() {
        Log.d("r/Runnable", "run")

        // カウントアップ
        countUp()

        // Runnable送信
        handler.postDelayed(this, 1000)
    }

    //--------------------------------------------------------------------------------------
    override fun onResume() {
        super.onResume()
        Log.d("app7/status", "onResume")

        // Runnable送信
        handler.postDelayed(this , 1000)
    }

    //--------------------------------------------------------------------------------------
    override fun onPause() {
        super.onPause()
        Log.d("app7/status", "onPause")

        // Runnable解除
        handler.removeCallbacks(this)
    }
}

実行結果

image.png

確認したバージョン

Android Studio Giraffe | 2022.3.1 Patch 2
Build #AI-223.8836.35.2231.10811636, built on September 15, 2023
Runtime version: 17.0.6+0-17.0.6b829.9-10027231 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 6.5.0-9-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 12
Registry:
    external.system.auto.import.disabled=true
    debugger.new.tool.window.layout=true
    ide.text.editor.with.preview.show.floating.toolbar=false
    ide.experimental.ui=true


Current Desktop: ubuntu:GNOME
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