본문 바로가기
개발/안드로이드 어플

DialogInterface.OnClickListener 인터페이스

by 외노자개발 2022. 12. 21.
반응형

 

DialogInterface.OnClickListener 인터페이스는 대화 상자에 포함된 버튼 클릭 처리에 사용됩니다.


클릭 시 호출되는 메서드는 'onClick' 메서드입니다.

 

public void onClick(DialogInterface dialog, int whichButton)
This method will be invoked when a button in the dialog is clicked.

Parameters:
  dialog The dialog that was cancelled will be passed into the method.
  whichButton The button that was clicked, ie BUTTON1 or BUTTON2.

첫 번째 인수에는 클릭이 발생한 'android.content.DialogInterface' 인터페이스를 구현하는 클래스의 객체가 전달됩니다. 

인터페이스를 구현한 클래스로는

「AlertDialog」클래스,「DatePickerDialog」클래스,「Dialog」클래스,「ProgressDialog」클래스,「TimePickerDialog」

가 있습니다. 이벤트 발생원의 다이얼로그를 판별하기 위해서 사용합니다.

 

두 번째 인수에는 클릭이 발생한 버튼 유형이 전달됩니다. 버튼 유형은 "android.content.DialogInterface'' 인터페이스에 정의되어 있으며 다음 중 하나입니다.

android.content.DialogInterface.BUTTON1
android.content.DialogInterface.BUTTON2

 

구현 방법


실제로 사용하려면 "android.content.DialogInterface.OnClickListener"인터페이스를 구현하여 클래스 내에서 "onClick"메소드를 정의합니다. 아래는 "AlertDialog"클래스를 사용한 경우입니다.

 

import android.app.AlertDialog;
import android.content.DialogInterface;

public class Test extends Activity implements DialogInterface.OnClickListener{

    @Override protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        AlertDialog.show(Test.this,
                         "Alert Test",
                         "Hello, This is Alert Dialog.",
                         "ok",
                         Test.this,
                         false,
                         null);
    }

    public void onClick(DialogInterface dialog, int whichButton) {
        if (whichButton == DialogInterface.BUTTON1){
            // 버튼 1 클릭시 처리
        }else if (whichButton == DialogInterface.BUTTON2){
            // 버튼 2 클릭시 처리
        }
    }
}

 

 

 

 

 

반응형

댓글