Android Application

Kotlin 고급 위젯 - 날짜와 시간 관련 위젯 : 데이트피커(DatePicker) Dialog

작성자 임베디드코리아 작성일23-09-24 00:50 조회1,511회 댓글0건

첨부파일

------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/start_text"
        style="bold"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="시 작"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/start_date_text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.147" />

    <TextView
        android:id="@+id/start_date_text"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_marginStart="60dp"
        android:gravity="center_vertical"
        android:paddingHorizontal="5dp"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/start_text"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.137" />

    <ImageView
        android:id="@+id/start_date_btn"
        android:layout_width="349dp"
        android:layout_height="414dp"
        tools:layout_editor_absoluteX="16dp"
        tools:layout_editor_absoluteY="236dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    //Calendar
    private var calendar = Calendar.getInstance()
    private var year = calendar.get(Calendar.YEAR)
    private var month = calendar.get(Calendar.MONTH)
    private var day = calendar.get(Calendar.DAY_OF_MONTH)

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

        var start_date_BTN: ImageView = findViewById(R.id.start_date_btn)
        var start_date_TEXT: TextView = findViewById(R.id.start_date_text)
        start_date_BTN.setOnClickListener {
            val datePickerDialog = DatePickerDialog(this, { _, year, month, day ->
                start_date_TEXT.text =
                    year.toString() + "/" + (month + 1).toString() + "/" + day.toString()
            }, year, month, day)
            datePickerDialog.show()
        }
    }
}