編集部 All About
Javaプログラミング関連情報
更新日:2008年04月01日
画面にメッセージを表示するダイアログの基本をマスターしましょう。
package jp.allabout.android;
import jp.allabout.android.R;
import android.app.*;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MyAndy extends Activity {
private Activity me;
TextView text;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
this.me = this;
// コンポーネントの設定
Button btn = (Button) this.findViewById(R.id.button);
text = (TextView)this.findViewById(R.id.text);
// イベント組み込み
btn.setOnClickListener(new BtnAdapter());
}
// イベントクラス
class BtnAdapter implements OnClickListener {
@Override
public void onClick(View v) {
AlertDialog.show(me, "Alert", "こんにちは!", "ok",
new OkAdapter(), false, null);
}
}
class OkAdapter implements
android.content.DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface arg0, int arg1) {
text.setText("OKしましたね!");
}
}
}
![]() |
| アラートのボタンをクリックすると、「OKしましたね!」と表示される。 |
AlertDialog.show([Context],タイトル,メッセージ,ボタン名,
ボタンクリック時のリスナー,キャンセル,キャンセル時のリスナー);
[Context]——ベースとなるContext(Activity)
タイトル——タイトルのテキスト
メッセージ——表示するメッセージ
ボタン名——ボタンに表示されるテキスト
ボタンクリック時のリスナー——ボタンをクリックしたときのOnClickListener
キャンセル——キャンセル可能かどうかを示すboolean値
キャンセル時のリスナー——キャンセル時のOnCancelListener
(執筆者:掌田 津耶乃)
この記事の担当ガイド

編集部 All About