DialogInterface.OnCancelListenerインターフェース

広告

「DialogInterface.OnCancelListener」インターフェースはダイアログが表示された時にキャンセルが発生した時の処理に使用されます。

android.content 
public static interface 
android.content.DialogInterface.OnCancelListener

イベント発生時に呼び出されるメソッドは「onCancel」メソッドとなります。

This method will be invoked when the dialog is cancelled.

Parameters:
  dialog  The dialog that was cancelled will be passed into the method.

1番目の引数にはクリックが発生した「android.content.DialogInterface」インターフェースをを実装したクラスのオブジェクトが渡されてきます。インターフェースを実装したクラスとしては「AlertDialog」クラス, 「DatePickerDialog」クラス, 「Dialog」クラス, 「ProgressDialog」クラス, 「TimePickerDialog」があります。イベント発生元のダイアログを判別するのに使用します。

実装方法

実際に利用するには「android.content.DialogInterface.OnCancelListener」インターフェースをインプリメントして、クラス内で「onCancel」メソッドを定義します。下記は「AlertDialog」クラスを使った場合です。

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

public class Test extends Activity implements 
    DialogInterface.OnClickListener, 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, 
                         true,
                         Test.this);
    }

    public void onClick(DialogInterface dialog, int whichButton) {
        // クリック時の処理
    }

    public void onCancel(DialogInterface dialog) {
        // キャンセル時の処理
    }
}

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。