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

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

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を表示することも可能です。
続きを読む…»

2,069 views