iOS를 사랑하는 AOS 개발자

[Android] 안드로이드 시간, 날짜 특집2 ( Calendar ) 본문

Android ( JAVA )/개발

[Android] 안드로이드 시간, 날짜 특집2 ( Calendar )

아사안개 2021. 12. 26. 00:41
반응형
SMALL

👼🏻 초보 안드로이드 개발자가 매번 구글링하기 싫어서 정리하는 블로그 👼🏻 

 

안녕하세요!!!

안드로이드 시간, 날짜 특집 2 탄입니다!!

 

 

[Android] 안드로이드 시간, 날짜 특집1 ( Stopwatch )

👼🏻 초보 안드로이드 개발자가 매번 구글링하기 싫어서 정리하는 블로그 👼🏻 안녕하세요!! 시간, 날짜에 관한것을 간단히 적어볼 예정이에요! 그 중 첫번째 버튼 A 클릭 후 버튼 B 를 누르

devziner.tistory.com

 

이번엔 캘린더를 사용해볼꺼에요!!

안드로이드는 기본 위젯으로 캘린더를 제공해줍니다!

 

요고요고 사용법 한번 가봅시다!

언제나 뷰부터!

 

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="20dp">

    <CalendarView
        android:id="@+id/calendar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/tv_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="날짜를 지정해 주세요!"
        android:gravity="center"
        android:textSize="20dp"/>
</LinearLayout>

(🤨 오 오늘은 짧다 짧아!!! )

 

이제 코드로 가볼까요?!

반응형
MainActivity
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // 위젯 전역변수
    CalendarView calendar;
    TextView tv_date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // xml 연결
        calendar = findViewById(R.id.calendar);
        tv_date = findViewById(R.id.tv_date);

        // Calendar 클릭 이벤트
        calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int day) {
                tv_date.setText(year + "년 " + (month + 1) + "월 " + day + "일 선택");
            }
        });
    }
}

코드도 짧게 되었군요!

 

calendar 의 클릭이벤트로 선택한 년, 월, 일 다 확인이 가능해요!

⚡️ month 에 + 1 하는 이유는 월이 0부터 시작되기때문에 꼭 +1 을 해야합니다!!!!!

 

저 메소드로 많은 곳에 활용이 가능하겠죠? ㅎㅎ

SMALL

구현 영상!!!!!!!!

 

 

 

( 🤭 만약 캘린더에 영어로 나온다면 애뮬의 언어설정을 한국어로 설정해주면 알아서 바뀔꺼에요! )

 

시간, 날짜 특집 2 끝!!!!!!!!!!!!!!!!!!!!!!!

반응형
LIST
Comments