반응형
DatePicker
설명
- 날짜 선택 기능을 제공하는 다이얼로그
코드
res/layout/activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/datePickerButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="open DatePicker" />
<Button
android:id="@+id/timePickerButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="open TimePicker" />
</LinearLayout>
MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val datePickerButton = findViewById<Button>(R.id.datePickerButton)
val timePickerButton = findViewById<Button>(R.id.timePickerButton)
datePickerButton.setOnClickListener {
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)
val listener = DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
Toast.makeText(this, "${year}년 ${month + 1}월 ${dayOfMonth}일", Toast.LENGTH_SHORT).show()
}
val picker = DatePickerDialog(this, listener, year, month, day)
picker.show()
}
timePickerButton.setOnClickListener {
val calendar = Calendar.getInstance()
val hour = calendar.get(Calendar.HOUR)
val minute = calendar.get(Calendar.MINUTE)
val listener = TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
Toast.makeText(this, "${hourOfDay}시 ${minute}분", Toast.LENGTH_SHORT).show()
}
val picker = TimePickerDialog(this, listener, hour, minute, false)
picker.show()
}
}
}
참고
반응형
'Development > Android' 카테고리의 다른 글
[Android] ListFragment (0) | 2021.02.09 |
---|---|
[Android] Fragment (0) | 2021.02.09 |
[Android] ListDialog (0) | 2021.02.09 |
[Android] ProgressDialog (0) | 2021.02.09 |
[Android] AlertDialog (0) | 2021.02.09 |