AlertDialog

Исходники и картинки для статьи Android: Диалоговое окно AlertDialog

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="AlertDialog с кнопками" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="AlertDialog со списком" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="AlertDialog с переключателями" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="AlertDialog с флажками" />

</LinearLayout>

MainActivity.java


package ru.alexanderklimov.test;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	AlertDialog.Builder ad;
	Context context;

	// идентификатор диалогового окна AlertDialog с кнопками
	private final int IDD_THREE_BUTTONS = 0;
	private final int IDD_LIST_CATS = 1;
	private final int IDD_RADIO_CATS = 2;
	private final int IDD_CHECK_CATS = 3;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		context = MainActivity.this;
		String title = "Выбор есть всегда";
		String message = "Выбери пищу";
		String button1String = "Вкусная пища";
		String button2String = "Здоровая пища";
		ad = new AlertDialog.Builder(context);
		ad.setTitle(title);
		ad.setMessage(message);
		ad.setPositiveButton(button1String, new OnClickListener() {
			public void onClick(DialogInterface dialog, int arg1) {
				Toast.makeText(context, "Вы сделали правильный выбор",
						Toast.LENGTH_LONG).show();
			}
		});
		ad.setNegativeButton(button2String, new OnClickListener() {
			public void onClick(DialogInterface dialog, int arg1) {
				Toast.makeText(context, "Возможно вы правы", Toast.LENGTH_LONG)
						.show();
			}
		});
		ad.setCancelable(true);
		ad.setOnCancelListener(new OnCancelListener() {
			public void onCancel(DialogInterface dialog) {
				Toast.makeText(context, "Вы ничего не выбрали",
						Toast.LENGTH_LONG).show();
			}
		});
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			// ad.show();
			showDialog(IDD_THREE_BUTTONS);
			break;
		case R.id.button2:
			showDialog(IDD_LIST_CATS);
			break;
		case R.id.button3:
			showDialog(IDD_RADIO_CATS);
			break;
		case R.id.button4:
			showDialog(IDD_CHECK_CATS);

		default:
			break;
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case IDD_THREE_BUTTONS:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Выберите правильный ответ")
					.setCancelable(false)
					.setPositiveButton("Мяу",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							})
					.setNeutralButton("Гав",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							})
					.setNegativeButton("Сам дурак!",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});

			return builder.create();

		case IDD_LIST_CATS:

			final String[] mCatsName = { "Васька", "Рыжик", "Мурзик" };

			builder = new AlertDialog.Builder(this);
			builder.setTitle("Выбираем кота"); // заголовок для диалога

			builder.setItems(mCatsName, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int item) {
					// TODO Auto-generated method stub
					Toast.makeText(getApplicationContext(),
							"Выбранный кот: " + mCatsName[item],
							Toast.LENGTH_SHORT).show();
				}
			});
			builder.setCancelable(false);
			return builder.create();

		case IDD_RADIO_CATS:

			final String[] mChooseCats = { "Васька", "Рыжик", "Мурзик" };
			builder = new AlertDialog.Builder(this);
			builder.setTitle("Выберите любимое имя кота")
					.setCancelable(false)

					// добавляем одну кнопку для закрытия диалога
					.setNeutralButton("Назад",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							})

					// добавляем переключатели
					.setSingleChoiceItems(mChooseCats, -1,
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int item) {
									Toast.makeText(
											getApplicationContext(),
											"Любимое имя кота: "
													+ mChooseCats[item],
											Toast.LENGTH_SHORT).show();
								}
							});
			return builder.create();

		case IDD_CHECK_CATS:
			final boolean[] mCheckedItems = { false, true, false };
			final String[] checkCatsName = { "Васька", "Рыжик", "Мурзик" };
			builder = new AlertDialog.Builder(this);
			builder.setTitle("Выберите котов")
					.setCancelable(false)

					.setMultiChoiceItems(checkCatsName, mCheckedItems,
							new DialogInterface.OnMultiChoiceClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which, boolean isChecked) {
									mCheckedItems[which] = isChecked;
								}
							})

					// Добавляем кнопки
					.setPositiveButton("Готово",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int id) {
									StringBuilder state = new StringBuilder();
									for (int i = 0; i < checkCatsName.length; i++) {
										state.append("" + checkCatsName[i]);
										if (mCheckedItems[i])
											state.append(" выбран\n");
										else
											state.append(" не выбран\n");
									}
									Toast.makeText(getApplicationContext(),
											state.toString(), Toast.LENGTH_LONG)
											.show();
								}
							})

					.setNegativeButton("Отмена",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();

								}
							});
			return builder.create();

		default:
			return null;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}