Android - 在不同的活动中使用一个DatePickerFragment

时间:2013-11-06 09:26:06

标签: android datepicker reusability

是否可以只有一个DatePickerFragment,可以在不同的活动中使用? 我的DatePickerFragment:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    final Calendar cal = new GregorianCalendar();
    int year;
    int month;
    int day;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker

    year = cal.get(Calendar.YEAR);
    month = cal.get(Calendar.MONTH);
    day = cal.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
    updateDate(year, month, day);
    }

    public void updateDate(int year, int month, int day) {
    this.year = year;
    this.month = month;
    this.day = day;
    cal.set(year, month, day);
    }

}

我用

找到了here
return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);

应该可以在Activiy中实现onDateSet。

public class MyActivity extends FragmentActivity implements DatePickerDialog.OnDateSetListener {
...
public void changeDate(View v) {
    log("Changing Date");
    DatePickerFragment datePickerFragment = new DatePickerFragment();
    datePickerFragment.show(getFragmentManager(), "datePicker");
}
public void onDateSet(DatePicker view, int year, int month, int day) {
    log(String.format("Date changed: %d-%d-%d", year, month, day));
}

但是onDateSet不会被执行。我的错是什么? 我只想要一个可以在不同活动中使用的单个可重用片段,而无需在每个活动中编写相同的代码。

1 个答案:

答案 0 :(得分:1)

为此,我还创建了一个扩展DialogFragment的DatePickerFragment。在这个类中,我使用了从父类传递的布局ID,如下所示:

    @Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    setCancelable(false);

    Bundle args = getArguments();
    id = args.getString("id");
    title = args.getString("title");
    layout = args.getInt("layout");
    message = args.getString("msg");

}

之后

      @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(layout, null);


    switch (layout) {

    case R.layout.date_picker:
        titleTxt = (TextView) view.findViewById(R.id.dtedlg_tv_heading);
        datePkr = (DatePicker) view.findViewById(R.id.dtedlg_dp_date);
        done = (Button) view.findViewById(R.id.dtedlg_btn_done);
        cancel = (Button) view.findViewById(R.id.dtedlg_btn_back);
        break;
          }

          done.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                    if (id.equals(ParentFragment.DATE_DIALOG)) { // checking

        Calendar value = Calendar.getInstance();
        value.set(Calendar.DAY_OF_MONTH, datePkr.getDayOfMonth());
        value.set(Calendar.MONTH, datePkr.getMonth());
        value.set(Calendar.YEAR, datePkr.getYear());

                    ParentFragment.from_date.setText(DateFormat
            .format("dd/MM/yyyy", value).toString());
                    }
                    getDialog().dismiss();
                 }
                }
            return view;
          }

遵循'date_picker'布局:

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="#FFFFFFFF"
   android:orientation="vertical" >

    <LinearLayout
    style="@style/DialogBanner.Solid.Apptheme"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:minHeight="40dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/dtedlg_tv_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Approve" />
   </LinearLayout>

   <DatePicker
    android:id="@+id/dtedlg_dp_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

   <LinearLayout
    style="@style/Dialog_LayoutFooter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right" >

    <Button
        android:id="@+id/dtedlg_btn_done"
        style="@style/App_ButtonStyleFooter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="@drawable/ic_action_done"
        android:minHeight="10dp"
        android:minWidth="10dp" />

    <Button
        android:id="@+id/dtedlg_btn_back"
        style="@style/App_ButtonStyleFooter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="5dp"
        android:drawableRight="@drawable/ic_action_back"
        android:minHeight="10dp"
        android:minWidth="10dp" />
    </LinearLayout>

   </LinearLayout>