カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

アラートダイアログを表示する

この記事は最終更新日から1年以上経過しています。
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setMessage("メッセージ");
ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
    //肯定ボタンの操作
  }
});
ad.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
    //中立ボタンの動作
  }
});
ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
    //キャンセルボタンの動作
  }
});
ad.create();
ad.show();

キャンセルボタン等で何もせず、ダイアログを閉じるだけの場合はリスナーにnullを指定します。

ad.setNegativeButton("Cancel", null);

setViewで任意のViewを表示することも可能です。

LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.layout_root));

AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setView(layout);
ad.setPositiveButton("OK", null);
ad.create();
ad.show();
この記事がお役に立ちましたらシェアお願いします
2,073 views

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です