ダイアログクラスAlertDialogには、あらかじめ3つのボタンが組み込まれており、インタフェースDialogInterfaceのonClickメソッドの第2引数にて、どのボタンが押されたか取得することができます。
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class MyClass extends Activity{
public void myMethod(){
// リスナーの作成
DialogInterface.OnClickListener lis = new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
switch(which){
case DialogInterface.BUTTON_POSITIVE:
case DialogInterface.BUTTON_NEUTRAL:
case DialogInterface.BUTTON_NEGATIVE:
}
}
};
// ダイアログの作成
AlertDialog.Builder bldr = new AlertDialog.Builder(this);
bldr.setTitle("Title");
bldr.setMessage("Message");
bldr.setPositiveButton("OK",lis); // 左ボタン
bldr.setNeutralButton("NG",lis); // 真ん中ボタン
bldr.setNegativeButton("キャンセル",lis); // 右ボタン
bldr.create();
bldr.show();
}
}
参考: ダイアログのカスタマイズ
参考: ダイアログのキャンセル・閉じる


コメント