アラートダイアログを表示する |
アラートを実行することにより、テキストとボタンを表示することができます。 .show()を忘れてしまうと、画面に表示されません。 package jp.goodroid.sample; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; public class AlertDialogSample extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //インスタンス化 AlertDialog.Builder alertSample = new AlertDialog.Builder( this ); //表示するタイトルを設定 alertSample.setTitle( "アラートタイトル" ); //表示するメッセージを設定 alertSample.setMessage( "アラートメッセージ" ); //アラートボタン alertSample.setPositiveButton( "OK" , null ); //アラートを表示 alertSample.show(); } } また、ボタンの選択値によって処理を変えたい場合は、 package jp.goodroid.sample; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; public class AlertDialogSample extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //インスタンス化 AlertDialog.Builder alertSample = new AlertDialog.Builder( this ); //表示するタイトルを設定 alertSample.setTitle( "アラートタイトル" ); //表示するメッセージを設定 alertSample.setMessage( "アラートメッセージ" ); //アラートOKボタン alertSample.setPositiveButton( "OK" , new DialogInterface.OnClickListener(){ public void onClick( DialogInterface dialog, int which ) { //OKが押された時の処理を記述します } }); //アラートNGボタン alertSample.setNegativeButton( "NG" , new DialogInterface.OnClickListener(){ public void onClick( DialogInterface dialog, int which ) { //NGが押された時の処理を記述します } }); //メモ //setPositiveButton() メソッドで設定したボタンは一番左に配置される。 //setNeutralButton() メソッドで設定したボタンは中央に配置される。 //setNegativeButton() メソッドで設定したボタンは一番右に配置される。 //アラートを表示 alertSample.show(); } } 実行結果は以下の通り。 ![]() ![]() |
3734 views | コメント:0 | 2012-02-26 |
コメント
|
|
まだこの記事にコメントはありません |