【Android】AlertDialogの表示(リストダイアログ)
リストダイアログの表示方法です。
ボタンを押すと、下記のようなダイアログを表示します。
●ソースコード
「R.layout.main」にボタンを配置し、
「android:onClick="BtnClick"」を設定し使用するソースです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | public class TestDialog extends Activity { public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); } //ボタンクリックイベント(XMLにandroid:onClickを記述) public void BtnClick(View v) { if (v.getId() == R.id.button0) { showDialog( 0 ); } } @Override protected Dialog onCreateDialog( int id) { // ダイアログが複数ある場合にidを使用する if (id == 0 ) { builder.setIcon(R.drawable.icon); builder.setTitle( "リストダイアログ" ); builder.setItems(R.array.test, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String[] itemList = getResources().getStringArray(R.array.test); TextView textview1 = (TextView)findViewById(R.id.textview1); textview1.setText(itemList[which]); } }); dialog = builder.create(); } } @Override protected void onPrepareDialog( int id, Dialog dialog) { super .onPrepareDialog(id, dialog); if (id == 0 ) { //ダイアログの内容に変更がある場合に記述 } } } |