目次
ダイアログとそのボタンを表示する。
ダイアログとボタンを表示する
- AlertDialog.Builder でダイアログを作成
- AlertDialog.Builder#set**Buttonメソッドでダイアログボタンをセット。** の部分は Positive, Neutral, Negative の3つ。
AlertDialog.Builder#showメソッドで表示
package jp.fernweh; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class ButtonAlertDialoger extends Activity { LinearLayout layout; TextView text; Button btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); setContentView(layout); text = new TextView(this); text.setText("テキストビュー"); layout.addView(text); btn = new Button(this); btn.setText("ダイアログを表示"); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { displayDialog(); } }); layout.addView(btn); } private void displayDialog() { // AlertDialog.Builder でダイアログを作成 AlertDialog.Builder dlg = new AlertDialog.Builder(this); // ダイアログのタイトルをセット dlg.setTitle("Test Dialog"); // ダイアログのメッセージをセット dlg.setMessage("Test Message"); // AlertDialog.Builder#setPositiveButton // OK とか YES とか。左に配置される dlg.setPositiveButton("おk", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { text.setText("『おk』を押しました"); } }); // AlertDialog.Builder#setNeutralButton // CANCEL とか。真ん中に配置される dlg.setNeutralButton("キャンセル", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { text.setText("『キャンセル』を押しました"); } }); // AlertDialog.Builder#setNegativeButton // No とか。右に配置される dlg.setNegativeButton("ダメ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { text.setText("『ダメ』を押しました"); } }); // これを忘れるとダイアログは表示されないよ dlg.show(); } }
項目を選択するダイアログを表示する
AlertDialog.Builder#setItems(CharSequence[] items, DialogInterface.OnClickListener listener)
package jp.fernweh;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ItemsAlertDialoger extends Activity {
LinearLayout layout;
TextView text;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
text = new TextView(this);
text.setText("テキストビュー");
layout.addView(text);
btn = new Button(this);
btn.setText("ダイアログを表示");
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
displayDialog();
}
});
layout.addView(btn);
}
private void displayDialog() {
AlertDialog.Builder dlg = new AlertDialog.Builder(this);
dlg.setTitle("Select Dialog");
// dlg_select.setMessage("message"); これを消してなくてハマった…
String[] items = { "One", "Two", "Three" };
dlg.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
text.setText("いち");
break;
case 1:
text.setText("に");
break;
case 2:
text.setText("さん");
break;
default:
text.setText("でふぉると");
break;
}
}
});
dlg.show();
}
}