added colors
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.example.hairdryer
|
||||
|
||||
import android.app.Activity
|
||||
import android.graphics.Color
|
||||
import android.media.AudioAttributes
|
||||
import android.media.SoundPool
|
||||
import android.os.Bundle
|
||||
@@ -39,6 +40,12 @@ class MainActivity : Activity() {
|
||||
|
||||
soundId = soundPool.load(this, R.raw.sound, 1)
|
||||
|
||||
// Initial: Start-Zustand (grün)
|
||||
btn.setBackgroundColor(Color.GREEN)
|
||||
btn.setTextColor(Color.BLACK)
|
||||
status.setBackgroundColor(Color.LTGRAY)
|
||||
status.setTextColor(Color.BLACK)
|
||||
|
||||
btn.setOnClickListener {
|
||||
if (isPlaying) {
|
||||
stopSound(btn, status)
|
||||
@@ -51,18 +58,22 @@ class MainActivity : Activity() {
|
||||
|
||||
private fun startSound(minutes: Int, btn: Button, status: TextView) {
|
||||
stopSound(btn, status) // sicherstellen, dass nichts doppelt läuft
|
||||
streamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f) // -1 = loop unendlich
|
||||
streamId = soundPool.play(soundId, 1f, 1f, 1, -1, 1f)
|
||||
isPlaying = true
|
||||
btn.text = "Stop"
|
||||
btn.setBackgroundColor(Color.RED)
|
||||
btn.setTextColor(Color.BLACK)
|
||||
|
||||
status.setBackgroundColor(Color.GREEN)
|
||||
status.text = if (minutes > 0) "Läuft: ${minutes} min" else "Läuft: ∞"
|
||||
|
||||
if (minutes > 0) {
|
||||
remainingMinutes = minutes
|
||||
updateStatus(status)
|
||||
countdownRunnable = object : Runnable {
|
||||
override fun run() {
|
||||
remainingMinutes--
|
||||
if (remainingMinutes > 0) {
|
||||
updateStatus(status)
|
||||
status.text = "Läuft: ${remainingMinutes} min"
|
||||
handler.postDelayed(this, 60_000L)
|
||||
} else {
|
||||
stopSound(btn, status)
|
||||
@@ -70,8 +81,6 @@ class MainActivity : Activity() {
|
||||
}
|
||||
}
|
||||
handler.postDelayed(countdownRunnable!!, 60_000L)
|
||||
} else {
|
||||
status.text = "Läuft: ∞"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,15 +88,16 @@ class MainActivity : Activity() {
|
||||
if (isPlaying) {
|
||||
soundPool.stop(streamId)
|
||||
isPlaying = false
|
||||
remainingMinutes = 0
|
||||
handler.removeCallbacks(countdownRunnable ?: Runnable { })
|
||||
status.text = "Gestoppt"
|
||||
btn.text = "Start"
|
||||
}
|
||||
}
|
||||
remainingMinutes = 0
|
||||
handler.removeCallbacks(countdownRunnable ?: Runnable { })
|
||||
status.text = "Gestoppt"
|
||||
status.setBackgroundColor(Color.RED)
|
||||
status.setTextColor(Color.BLACK)
|
||||
|
||||
private fun updateStatus(status: TextView) {
|
||||
status.text = "Läuft: ${remainingMinutes} min"
|
||||
btn.text = "Start"
|
||||
btn.setBackgroundColor(Color.GREEN)
|
||||
btn.setTextColor(Color.BLACK)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
Reference in New Issue
Block a user