Android/Kotlin

[Android] DatePicker, TimePicker

lipnus 2018. 7. 18. 17:14
반응형



month는 0부터 시작하므로 +1을 해준 것.


class MainActivity : AppCompatActivity() {

var cal = Calendar.getInstance()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

btn_datepicker.setOnClickListener{
showDatePicker();
}

btn_timepicker.setOnClickListener{
showTimePicker();
}
}

fun showDatePicker(){
DatePickerDialog(this, DatePickerDialog.OnDateSetListener{datePicker, year, month, day ->
println(year)
println(month + 1)
println(day)
}, cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DATE)).show();
}

fun showTimePicker(){
TimePickerDialog(this, TimePickerDialog.OnTimeSetListener{timePicker, hour, minute ->
println(hour)
println(minute)
}, cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), true).show()
}
}


반응형

'Android > Kotlin' 카테고리의 다른 글

Class - 기본  (0) 2018.11.20
IntelliJ IDEA에서 Kotlin실행  (0) 2018.11.20
Recyclerview(LinearLayout)  (0) 2018.11.19
RecyclerView(GridLayout)  (0) 2018.11.19
코틀린 기초  (0) 2018.07.13