カテゴリー:
睡眠導入
閲覧数:311 配信日:2018-05-07 12:27
コード
▼MainActivity.kt
package work.w4c.android0.introducesleep20180426
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
class MainActivity : AppCompatActivity() {
var sheepCount = 0
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
rootLayout.setOnClickListener { //追加
sheepCount++ //追加
val sheepText = "ひつじが$sheepCount 匹" //追加
textview.text = sheepText //追加
} //追加
}
}
・初めてのAndroidアプリ開発(Kotlin編)