AlertDialogにViewを設定することで、カスタマイズしたダイアログを表示できます。
AlertDialogに設定するViewのlayoutのxmlは以下の通りです。
#codeprettify{{
?xml version="1.0" encoding="utf-8"?>
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <DatePicker android:id="@+id/datepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/timepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" />
/LinearLayout> }} 上記のレイアウトは、DatePickerとTimePickerを持つViewです。
以下の処理で、AlertDialogで入力した内容を取得できます。
#codeprettify{{
private View dateTimeView = null;
・・・
public boolean onPreferenceClick(Preference preference) { LayoutInflater inf = LayoutInflater.from(this); dateTimeView = inf.inflate(R.layout.datetimedialog, (ViewGroup)findViewById(R.id.layout_root)); AlertDialog.Builder dlg = null; dlg = new AlertDialog.Builder(this); dlg.setTitle("日時設定"); dlg.setView(dateTimeView); dlg.setPositiveButton(R.string.lb_grp_btn_ok, this); dlg.setNegativeButton(R.string.lb_grp_btn_cancel, this); dlg.show(); return true; }
@Override public void onClick(DialogInterface dialog, int which) { switch(which){ case Dialog.BUTTON_POSITIVE: DatePicker dp = (DatePicker)dateTimeView.findViewById(R.id.datepicker); TimePicker tp = (TimePicker)dateTimeView.findViewById(R.id.timepicker); break; case Dialog.BUTTON_NEGATIVE: break; } }
}}