프로그래밍/안드로이드

안드로이드 - CalendarView로 쉽게 달력을 만들자

가카리 2015. 8. 23. 18:56
반응형

 

CalendarView는 스크롤이 가능한 달력을 보여주며 날짜 하나를 선택 받을 수 있다.

 

속성

설명

firstDayOfWeek

제일 왼쪽의 첫 요일을 선택

minDate

달력에 표시할 최소 날짜. mm/dd/yyyy로 지정

maxDate

달력에 표시할 최대 날짜. mm/dd/yyyy로 지정

focusedMonthDateColor

현재 선택된 달의 배경 색상이다.

selectedWeekBackgroundColor

선택된 주의 배경 색상이다.

unfocusedMonthDateColor

선택되지 않은 달의 배경 색상이다.

showWeekNumber

왼쪽에 주차를 보여줄지

weekNumberColor

주차의 색상을 지정

weekSeparatorLineColor

주 사이의 구분선

selectedDateVerticalBar

선택한 날짜의 양쪽에 보일 수직바에 대한 이미지

 

 

생성 직후에 자동으로 오늘 날짜로 설정된다.

 

long getDate() : 날짜 조사하는 메소드

void setDate(long date [, boolean animate, boolean center]) : 현재 날짜 변경 시 애니메이션이나 중앙에 오게 할 수 있다.

 

날짜가 변경될때 이벤트를 받으려면 다음 메소드로 리스너를 등록한다.

 

void setOnDateChangeListener(CalendarView,OnDateChangeListener listener)

void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth)

 

 

main.xml 코드

 

<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"

    >

<CalendarView

android:id="@+id/calendar"

android:layout_width="match_parent"

android:layout_height="0px"

android:layout_weight="1"

/>

<CalendarView

android:layout_width="match_parent"

android:layout_height="0px"

android:layout_weight="1"

android:minDate="04/01/2013"

android:maxDate="12/31/2013"

android:focusedMonthDateColor="#ff0000"

android:unfocusedMonthDateColor="#0000ff"

android:selectedWeekBackgroundColor="#ffff00"

android:showWeekNumber="false"

android:weekSeparatorLineColor="#00ff00"

/>

 

</LinearLayout>

 

 

자바 파일

CalendarViewTest.java

 

package com.example.ch13_calendarview;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.CalendarView;

import android.widget.Toast;

 

 

public class CalendarViewTest extends Activity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

      

        //CalendarView 인스턴스 만들기

        CalendarView calendar = (CalendarView)findViewById(R.id.calendar);

        

        //리스너 등록

        calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

            

            @Override

            public void onSelectedDayChange(CalendarView view, int year, int month,

                    int dayOfMonth) {

                // TODO Auto-generated method stub

                Toast.makeText(CalendarViewTest.this, ""+year+"/"+(month+1)+"/"

                        +dayOfMonth, 0).show();

            }

        });

        

    }

 

 

    

}

  

실행 화면

 

 

반응형