h1.목적 : 폼 위젯의 이벤트 처리 방법을 익힌다.

참조 : 클릭이벤트
h2.클릭 이벤트 구현방법

XML 이용

예> 버튼 이벤트

 
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage" />


android:onClick 으로 호출되는 메소드의 특징

  • Be public
  • Return void
  • Define a View as its only parameter (this will be the View that was clicked)
 
/** Called when the user touches the button */
public void sendMessage(View view) {
    // Do something in response to button click
} 

프로그램 코딩

  1. View.OnClickListener 객체 생성
  2. setOnClickListener(View.OnClickListener) 구현

익명클래스 이용

 
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

protected void onCreate(Bundle savedValues) {
    ...
    // Capture our button from layout
    Button button = (Button)findViewById(R.id.corky);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(mCorkyListener);
    ...
}

액티비티 자체 구현


public class ExampleActivity extends Activity implements OnClickListener {
    protected void onCreate(Bundle savedValues) {
        ...
        Button button = (Button)findViewById(R.id.corky);
        button.setOnClickListener(this);
    }

    // Implement the OnClickListener callback
    public void onClick(View v) {
      // do something when the button is clicked
    }
    ...
}

h2.Event Listeners
이벤트 리스터는 View 클래스의 인터페이스로 단일 콜백 메소드를 가지고 있다.
이 메소드는 사용자와의 상호작용에 의한 트리거가 발생되면 Android framework 에 의해 호출된다.

참고 Input Events

이벤트 리스너의 콜백 메소드

  • onClick()
    From View.OnClickListener. This is called when the user either touches the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses the suitable "enter" key or presses down on the trackball.
  • onLongClick()
    From View.OnLongClickListener. This is called when the user either touches and holds the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses and holds the suitable "enter" key or presses and holds down on the trackball (for one second).
  • onFocusChange()
    From View.OnFocusChangeListener. This is called when the user navigates onto or away from the item, using the navigation-keys or trackball.
  • onKey()
    From View.OnKeyListener. This is called when the user is focused on the item and presses or releases a hardware key on the device.
  • onTouch()
    From View.OnTouchListener. This is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item).
  • onCreateContextMenu()
    From View.OnCreateContextMenuListener. This is called when a Context Menu is being built (as the result of a sustained "long click"). See the discussion on context menus in the Menus developer guide.
    These methods are the sole inhabitants of their respective interface. To define one of these methods and

h2.실습과제


	public static class DatePickerFragment extends DialogFragment {	

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			// Use the current time as the default values for the picker
			
			final Calendar c = Calendar.getInstance();
			int year 	= c.get(Calendar.YEAR);
			int month 	= c.get(Calendar.MONTH);
			int day 		= c.get(Calendar.DAY_OF_MONTH);
			
			// Create a new instance of DatePickerDialog and return it
	        return new DatePickerDialog(getActivity(), (MainActivity)getActivity(), year, month, day);
		}
		

	}//end DatePickerDialog
	



	
	/**
	 *  데이트피커 선택 후 처리
	 */
    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
    	
    	//edt_birthday.setText(new StringBuilder().append(year).append("-")
//    			.append(month+1).append("-").append(day));
    }