aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/miz_hi/smileessence/dialog/ConfirmDialog.java
blob: ed4c290a4527f40590eb6ae430df7a905f4c29d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package net.miz_hi.smileessence.dialog;

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

public class ConfirmDialog
{

	private Activity activity;
	private String textTitle;
	private String textPositive = "はい";
	private String textNegative = "いいえ";
	private OnClickListener listener;

	public ConfirmDialog(Activity activity, String title)
	{
		this.activity = activity;
		this.textTitle = title;
	}

	public void setTitle(String title)
	{
		this.textTitle = title;
	}

	public void setTextPositive(String textPositive)
	{
		this.textPositive = textPositive;
	}

	public void setTextNegative(String textNegative)
	{
		this.textNegative = textNegative;
	}

	public void setOnClickListener(OnClickListener listener)
	{
		this.listener = listener;
	}
	
	public AlertDialog createYesNoAlert()
	{
		AlertDialog.Builder ad = new AlertDialog.Builder(activity);
		ad.setTitle(textTitle);
		ad.setCancelable(false);
		ad.setPositiveButton(textPositive, listener);
		ad.setNegativeButton(textNegative, listener);
		return ad.create();
	}
	
	public static void show(Activity activity, String text, final Runnable onYes)
	{
		show(activity, text, onYes, null);
	}
	
	public static void show(Activity activity, String text, final Runnable onYes, final Runnable onNo)
	{
		ConfirmDialog helper = new ConfirmDialog(activity, text);
		OnClickListener listener = new OnClickListener()
		{
			
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				switch (which)
				{
					case DialogInterface.BUTTON_POSITIVE:
					{
						onYes.run();
						break;
					}
					case DialogInterface.BUTTON_NEGATIVE:
					{
						if(onNo != null)
						{
							onNo.run();
						}
						dialog.dismiss();
						break;
					}
				}
			}
		};
		helper.setOnClickListener(listener);
		helper.createYesNoAlert().show();
	}

}