事前準備

次のプロジェクトをEclipseにインポートして下さい。
ファイル名practiceAlertDialog.zip

演習1: アイコン付きのAlertDialog

src/jp.co.example.alertDialog/PracticeAlertDialogActivity.javaに、次のメソッドを追加して下さい。
// アイコン付きダイアログ
public void iconDialog() {
	new AlertDialog.Builder(PracticeAlertDialogActivity.this)
		// タイトルの設定
		.setTitle("エラー発生")
		// 本文の設定
		.setMessage("もう一度実行しますか?")
		// アイコンの設定
		.setIcon(R.drawable.error)
		// 肯定ボタンの設定
		.setPositiveButton("はい",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						mTextView.setText("「はい」が選択されました");「
					}
				})
		// 否定ボタンの設定
		.setNegativeButton("いいえ",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						mTextView.setText("「いいえ」が選択されました");
					}
				})
		// ダイアログの表示
		.show();
}
実行し、「アイコン付き」の行をタップして下さい。次のように表示されましたか。

演習2: 単一選択のAlertDialog

src/jp.co.example.alertDialog/PracticeAlertDialogActivity.javaに、次のメソッドを追加して下さい。
// 単一選択ダイアログ
public void singleChoiceDialog() {
	new AlertDialog.Builder(PracticeAlertDialogActivity.this)
		// タイトルの設定
		.setTitle("好きな果物は?")
		// アイコンの設定
		.setIcon(android.R.drawable.ic_dialog_info)
		// 設問(ラジオボタン)の設定
		.setSingleChoiceItems(items, -1, // デフォルトの選択
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						mTextView.setText(items[which]
								+ " が選択されました");
					}
				})
		// 肯定ボタンの設定
		.setPositiveButton("決定", null)
		// ダイアログの表示
		.show();
}
実行し、「単一選択」の行をタップして下さい。次のように表示されましたか。

演習3: 複数選択のAlertDialog

src/jp.co.example.alertDialog/PracticeAlertDialogActivity.javaに、次のメソッドを追加して下さい。
// 複数選択ダイアログ
public void multiChoiceDialog() {
	new AlertDialog.Builder(PracticeAlertDialogActivity.this)
		// タイトルの設定
		.setTitle("好きな果物は?")
		// アイコンの設定
		.setIcon(android.R.drawable.ic_dialog_info)
		// 設問(チェックボックス)の設定
		.setMultiChoiceItems(items, itemsChecked,
				new DialogInterface.OnMultiChoiceClickListener() {
					@Override
					public void onClick(DialogInterface dialog,
							int which, boolean isChecked) {
					}
				})
		// 肯定ボタンの設定
		.setPositiveButton("決定",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						StringBuilder sb = new StringBuilder();
						for (int i = 0; i < items.length; i++) {
							if (itemsChecked[i]) {
								sb.append(items[i] + ",");
							}
						}
						String value = "";
						if (sb.length() > 0) {
							value = sb.substring(0, sb.length() - 1);
							mTextView.setText("好きな果物は、" + value
									+ " です");
						} else {
							mTextView.setText("好きな果物はないようです");
						}
					}
				})
		// ダイアログの表示
		.show();
}
実行し、「複数選択」の行をタップして下さい。次のように表示されましたか。

演習4: カスタムレイアウトのAlertDialog

src/jp.co.example.alertDialog/PracticeAlertDialogActivity.javaに、次のメソッドを追加して下さい。
// カスタムダイアログ
public void customDialog() {
	// テキストボックスの取得
	LayoutInflater inflater = LayoutInflater
			.from(PracticeAlertDialogActivity.this);
	View row = inflater.inflate(R.layout.dialog_edit_text, null);
	final EditText editText = (EditText) row.findViewById(R.id.edit);

	new AlertDialog.Builder(PracticeAlertDialogActivity.this)
		// タイトルの設定
		.setTitle("メールアドレス設定")
		// アイコンの設定
		.setIcon(android.R.drawable.ic_dialog_email)
		// カスタムレイアウトのセット
		.setView(row)
		// 肯定ボタンの設定
		.setPositiveButton("決定",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						mTextView.setText("email: " + editText.getText()
								.toString());
					}
				})
		// ダイアログの表示
		.show();
}
実行し、「テキストボックス」の行をタップして下さい。次のように表示されましたか。


以上で本日の演習は終了です。おつかれさまでした。

このページへのコメント

oEkUo2 <a href="http://irifcghhephf.com/">irifcghhephf</a>, [url=http://lgxzcazbpjaw.com/]lgxzcazbpjaw[/url], [link=http://pnizdzwhbedx.com/]pnizdzwhbedx[/link], http://vnzcyedvlbjf.com/

0
Posted by sjpjssmk 2013年11月14日(木) 16:33:10 返信

cuppycake keybank adil

0
Posted by can't 2013年10月03日(木) 06:47:06
http://download.rar.password.unlocker.3.0.software...
返信

Lc0SwJ <a href="http://quzajnfodgoh.com/">quzajnfodgoh</a>, [url=http://fxukpwhptruz.com/]fxukpwhptruz[/url], [link=http://btpqjyhufysl.com/]btpqjyhufysl[/link], http://cdrpbeewrwml.com/

0
Posted by ehemptfipn 2013年09月22日(日) 17:10:29 返信

AAk6SE <a href="http://rkgdyazqltkx.com/">rkgdyazqltkx</a>, [url=http://sehcpinzafyr.com/]sehcpinzafyr[/url], [link=http://ndvvmqzpfomv.com/]ndvvmqzpfomv[/link], http://xzyjmrsfjezw.com/

0
Posted by safcetviedc 2013年09月22日(日) 05:45:00
http://yspcjwwmvbgi.com/
返信

4KmmXp <a href="http://knxdubpzwupl.com/">knxdubpzwupl</a>, [url=http://bmlatpyqauwx.com/]bmlatpyqauwx[/url], [link=http://iqznhujlmzbv.com/]iqznhujlmzbv[/link], http://vdibcckfjvox.com/

0
Posted by qeenxlb 2013年07月07日(日) 05:33:32
http://racjfelogrgx.com/
返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

管理人/副管理人のみ編集できます