AlertDialogにViewを設定する

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> }} 上記のレイアウトは、DatePickerTimePickerを持つ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;
       }
   }

}}

参考


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2011-12-02 (金) 18:06:24