アラートダイアログを表示する
- Android
-
2011-12-03 - 更新:2012-05-19
この記事は最終更新日から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,324 views




