본문 바로가기

모바일앱/안드로이드

[안드로이드] 이벤트를 처리하는 2가지 방법

콜백메서드를 재정의한다.



- 이벤트를 처리할 메서드를 미리 정의해둔다. View는 다음 4가지 콜백 메서드가 정의되어 있다. CustomView 생성시 필요한 메서드를 재정의한다.

onKeyDown(int,KeyEvent)

onKeyUp(int, KeyEvent)

onTrackballEvent(MotionEvent)

onTouchEvent(MotionEvent)


리스너 인터페이스를 구현한다.



- 리스너는 View클래스의 inner interface로 정의되어 있다. 

  리스너 인터페이스를 구현한 클래스를 정의하거나 이벤트를 처리할 액티비티 클래스를 선언할 때 implements 키워드로 해당 인터페이스를 구현하도록 한다.

해당 이벤트를 처리할 뷰에 리스너객체를 연결한다.

     view.setOnXXXXXListener(this) 또는

     view.setOnXXXXXListener(new OnXXXXXListener())



View.OnAttachStateChangeListener.onAttachedToWindow(View)

                                              .onDeattachedromWindow(View)

View.OnClickListener.onClick(View)

View.OnCreateContextMenuListener.onCreateContextMenu(ContextMenu,View,

                                                                                        ContextMenu.ContextMenuInfo)

View.OnDragListener.onDrag(View,DragEvent)

View.OnFocusChangeListener.onFocusChange(View, boolean)

View.OnGenericMotionListener.onGenericMotion(View, MotionEvent)

View.OnHoverListener.onHover(View, MotionEvent)

View.OnKeyListener.onKey(View, int keycode, MotionEvent)

View.OnLayoutChangeListener.onlayoutChange(View, int left, int top, int right, int bottom, 

                                                                       int oldLeft, int oldTop, int oldRight, int oldBottom)

View.OnLongClickListener.onLongClick(View)

View.OnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(int visibility)

View.OnTouchListener.onTouch(View, MotionEvent)