jurano강의 lec5 android_annotations_event_binding

28
AndroidAnnotations Event binding 작작작 – 작작작 작작 작작작 : 151202

Upload: ju-ri-seo

Post on 15-Apr-2017

205 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Jurano강의 lec5 android_annotations_event_binding

AndroidAnnotationsEvent binding

작성자 – 김태희 최종 수정일 : 151202

Page 2: Jurano강의 lec5 android_annotations_event_binding

Event binding

오늘 말씀드릴 Event binding 은 안드로이드에서 제공하는 Event handling 을

쉽고 빠르게 사용할 수 있도록 해 줍니다 .

간단하게 OnClickListener 와그 친구들을 생각하시면 됩니다 .

Page 3: Jurano강의 lec5 android_annotations_event_binding

Event binding구성@TextChange@AfterTextChange@BeforeTextChange@EditorAction@FocusChange@CheckedChange@Touch@Click@LongClick@ItemClick@ItemLongClick@ItemSelect@OptionsItem@SeekBarProgressChange@SeekBarTouchStart@SeekBarTouchStop

본 강에서는 이 중 자주 쓰이는 @Click annotation 을 활용한 예제 어플리케이션을 통해 사용법을 익혀보도록 합니다 .

Page 4: Jurano강의 lec5 android_annotations_event_binding

@Click

왼쪽과 같은 간단한 버튼들에 이벤트를 등록하는 코드를 생각해 봅시다 .https://github.com/JuranoSaurus/EventbindingSample

Page 5: Jurano강의 lec5 android_annotations_event_binding

@Click

View.OnClickListener 인터페이스를 사용해야 하죠 .

구현 예시를 볼까요 ?

Page 6: Jurano강의 lec5 android_annotations_event_binding

@Click 버튼이 많으므로

여기서는 각각의 익명 클래스를 선언하지 않겠습니다 .

이 다음 페이지에 나올 메소드가 onClick()이라는 게 유추되시죠 ?

Page 7: Jurano강의 lec5 android_annotations_event_binding

@Click

맞습니다 .

switch~case 를 통해 분기되도록 하는 일반적인 이벤트 핸들링입니다 .

Page 8: Jurano강의 lec5 android_annotations_event_binding

@Click

맞습니다 .

switch~case 를 통해 분기되도록 하는 일반적인 이벤트 핸들링입니다 .

Page 9: Jurano강의 lec5 android_annotations_event_binding

@Click

이번에는 AA 가 적용된 코드를 보시죠 .

Page 10: Jurano강의 lec5 android_annotations_event_binding

@Click

가장 달라지는 부분은 핸들러 메소드 하나로 해결한다는 점입니다 .

게다가 , 버튼에 대한 선언이 없다는 것 !

Page 11: Jurano강의 lec5 android_annotations_event_binding

@Click

btnFirst 의 예처럼 핸들러 메소드와 리소스 ID 를 매칭해줄 수 있습니다 .

그런데 , 그 밑에는 없네요 ? 어떤 규칙으로 바인드 되는거죠 ?

Page 12: Jurano강의 lec5 android_annotations_event_binding

@Click

btnSecond 는 리소스 ID 와 매칭되는 것입니다 . 이와 같이 메소드 이름과 리소스 ID 를 같게 하여 암시적으로 바인딩할 수 있습니다 .

Page 13: Jurano강의 lec5 android_annotations_event_binding

@Click

이와 같은 기능을 AA에서는 InferringIDFromMethodName 이라고 부르고 있습니다 !

Page 14: Jurano강의 lec5 android_annotations_event_binding

@Click

Q. 질문 ! btn_third 는 뭐죠 ? btnThird 와는 다른데요 ?

Page 15: Jurano강의 lec5 android_annotations_event_binding

@Click

A. 리소스 ID 에는 snake_case 를 주로 사용하고 , 메소드 이름에는 camelCase 를 주로 사용합니다 . 그래서 AA 는 snake_case 와 camelCase 간에도 자동으로 매칭해 줍니다 .

Page 16: Jurano강의 lec5 android_annotations_event_binding

@Click

btnFourthClicked() 는 여러분이 예상하시는 것과 같이 btn_fourth 와 바인드됩니다 .

Page 17: Jurano강의 lec5 android_annotations_event_binding

@Click

@Click 이기 때문에 메소드 뒤에 접미사 Clicked 가 붙어도 자동으로 바인드합니다 .

Page 18: Jurano강의 lec5 android_annotations_event_binding

@Click

뒤에서 살펴볼 @Touch와 @LongClick 도 마찬가지로 ~Touched()와 ~LongClicked() 를 지원하니 참고해 두세요 .

Page 19: Jurano강의 lec5 android_annotations_event_binding

@Click

그럼 여기서 , btn_fourth 와 btn_fourth_clicked 버튼이 있고 , btnFourthClicked() 메소드가 있다면 어떤 버튼이 바인드될까요 ?

Page 20: Jurano강의 lec5 android_annotations_event_binding

@Click

답은 , “ 정의되지 않으므로 그렇게 사용해서는 안 된다” 입니다 .

실제로 바인드되는것은 btn_fourth이지만 이는 예측할 수 없는 결과입니다 .

Page 21: Jurano강의 lec5 android_annotations_event_binding

@Click

만능이 아니라는 것에 실망하셨나요 ?

하지만 명시적인 표현을 사용하는 습관을 가지도록 합시다 ^_^

Page 22: Jurano강의 lec5 android_annotations_event_binding

@Click

다음은 클릭된 View 를 인자로 받아오는 방법입니다 .

Button 의 상위 클래스인 View 로 전달이 되니 버튼으로 사용하려면 캐스팅이 필요합니다 .

Page 23: Jurano강의 lec5 android_annotations_event_binding

@Click

버튼이 많을 때 , 핸들러를 매 ID 마다 선언해야 한다면 너무 복잡해질 수 있습니다 .

이럴 때 , Annotation 에 그룹으로 리소스 ID를 여러 개 전달할 수 있습니다 . Annotation에서는 집합을 { 중괄호 } 로 묶어야 한다는 점 기억하세요 !

Page 24: Jurano강의 lec5 android_annotations_event_binding

@Click

AA 51 라인원본 74 라인

Page 25: Jurano강의 lec5 android_annotations_event_binding

@Click

Generate 된 부분을 보시죠 .

Page 26: Jurano강의 lec5 android_annotations_event_binding

@Click 우리가 작성한

AAClickSampleActivity.java가 다음과 같이 자동 생성된 모습입니다 .

왜 판별하기 모호한 이름을 사용하면 안되는지 추측해볼 수 있는 코드입니다 .

익숙한 코드죠 ?

Page 27: Jurano강의 lec5 android_annotations_event_binding

@More

즐거우셨나요 ? ^_^제공된 예제 어플리케이션을 통해 다른 annotation 의

활용 예시를 만나보실 수 있습니다 !https://github.com/JuranoSaurus/EventbindingSample

다음 편에는 Threading 에 관련된 annotation 들을다루도록 하겠습니다 .

Page 28: Jurano강의 lec5 android_annotations_event_binding

Thank you