Android Application

Kotlin 고급 위젯 - 날짜와 시간 관련 위젯 : 캘린더뷰(CalendarView)

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

첨부파일

------------------------------------------------------------------------------------
      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="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <CalendarView
        android:id="@+id/calendar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:focusedMonthDateColor="#000000"
        android:selectedWeekBackgroundColor="#ffffff"
        android:unfocusedMonthDateColor="#CFCACA"
        android:layout_weight="5">

    </CalendarView>

    <TextView
        android:id="@+id/displayTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Kotlin"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="100dp"
        android:layout_gravity="center" />

</LinearLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    private lateinit var calendarV: CalendarView
    private lateinit var diaryTextView: TextView

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

        /* 캘린더뷰 초기화 */
        calendarV= findViewById(R.id.calendar)

        /* 텍스트뷰 초기화 */
        diaryTextView= findViewById(R.id.displayTV)

        calendarV.setOnDateChangeListener { view, year, month, dayOfMonth ->
            diaryTextView.visibility = View.VISIBLE

            diaryTextView.text = String.format("%d / %d / %d", year, month + 1, dayOfMonth)

        }

    }

}