2011年6月10日金曜日

アラートダイアログにボタンを表示


package com.alertDialogText;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class AlertDialogText extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // ダイアログのオブジェクト(dlg) 生成
  AlertDialog.Builder dlg = new AlertDialog.Builder(this);

  // ダイアログにタイトルを指定
  dlg.setTitle("タイトル");

  // ダイアログにメッセージを指定
  dlg.setMessage("ダイアログのメッセージ");

  // ダイアログにアイコンを指定
  dlg.setIcon(R.drawable.icon);
  
  // アラートダイアログのポジティブボタンがクリックされた時に呼び出されるコールバックリスナーを登録します
  dlg.setPositiveButton("ポジティブ", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    //ボタンが押された時の処理
   }
  });

  // ダイアログを表示
  dlg.show();
 }
}

関連記事

0 件のコメント:

コメントを投稿