カテゴリー:
睡眠導入
閲覧数:309 配信日:2018-05-15 11:35
コード
▼/MainActivity.kt
package work.w4c.android0.introducesleep20180426
import android.media.MediaPlayer
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : AppCompatActivity() {
var sheepCount = 0
var mp: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var greeting = "こんにちは"
val trialTime = Date()
val calendar = GregorianCalendar()
calendar.time = trialTime
val hour = calendar.get(Calendar.HOUR_OF_DAY)
if (hour in 1..11) {
greeting = "おはよう"
} else if (15 < hour) {
greeting = "こんばんは"
}
//greeting += "。。。ひつじが1匹。。。ひつじが2匹。。。ひつじが3匹。。。"
//val sheepNum = 10
//for (i in 1..sheepNum) {
// greeting += "。。。ひつじが" + i + "匹"
//}
greeting += "ねむれませんか?"
textview.text = greeting
//findViewById<TextView>(R.id.textview).text = greeting
mp = MediaPlayer.create(applicationContext, R.raw.sheep_cry1)
rootLayout.setOnClickListener {
sheepCount++
val sheepText = "ひつじが$sheepCount 匹"
textview.text = sheepText
when(sheepCount % 2) {
0 -> {
imageView1.setImageResource(R.drawable.sheep_1)
mp?.start()
}
else -> imageView1.setImageResource(R.drawable.sheep_2)
}
}
}
// ↓ここに追加
override fun onPause() {
super.onPause()
Log.i("onPause", "眠るまでの回数=" + sheepCount)
}
}