Android Datepicker Fragment - 当用户设置日期时如何做某事?

dtc*_*dtc 16 android datepicker android-fragments

我在这里按照教程:http: //developer.android.com/guide/topics/ui/controls/pickers.html

我得到了工作,日期选择器出现了.但是,我被困在如何在用户设置日期时采取行动.

我看到他们有这种方法:

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我在其中添加任何代码,则无效.

我不熟悉Java所以我不确定需要哪些额外的代码来更新具有所选日期的textview.

我确实添加了这段代码:

private DatePickerDialog.OnDateSetListener mDateListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int month, int day) {
                updateDate(year, month, day);
            }
        };

public void updateDate(int year, int month, int day) {
    TextView bdate = (TextView)findViewById(R.id.birthDate);
    Date chosenDate = new Date(year, month, day);
    java.text.DateFormat dateFormat = DateFormat.getDateFormat(getApplicationContext());
    bdate.setText(dateFormat.format(chosenDate));
}
Run Code Online (Sandbox Code Playgroud)

但这没有任何作用.

我确实发现了这个几乎完全正确的问题: 使用dialogfragment从datepicker获取日期

但是,在该解决方案中,不会使用(EditSessionActivity)将DatePickerFragment类限制为仅使用EditSessionActivity?

GLe*_*Lee 9

您应该使用静态工厂将参数传递给DialogFragment.从文档中,

"每个片段必须有一个空构造函数,因此可以在恢复其活动状态时进行实例化.强烈建议子类没有其他带参数的构造函数,因为在重新实例化片段时不会调用这些构造函数;而是,参数可以由调用者使用setArguments(Bundle)提供,稍后由Fragment使用getArguments()检索."

类似地,Fragment子类应该是静态的,以便它可以在不依赖父活动或Fragment的情况下重新实例化自身.如果您不遵守这些准则,则在Fragment尝试恢复时有时会出现崩溃或错误.

从Android的文档构建DialogFragment的推荐方法是使用静态工厂方法.这是一个日期选择器片段的示例,它接受一个初始日期和一个OnDateChangedListener:

public static class DatePickerFragment extends DialogFragment{

    private OnDateSetListener onDateSetListener;

    static DatePickerFragment newInstance(Date date, OnDateSetListener onDateSetListener) {
        DatePickerFragment pickerFragment = new DatePickerFragment();
        pickerFragment.setOnDateSetListener(onDateSetListener);

        //Pass the date in a bundle.
        Bundle bundle = new Bundle();
        bundle.putSerializable(MOVE_IN_DATE_KEY, date);
        pickerFragment.setArguments(bundle);
        return pickerFragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreateDialog(savedInstanceState);

        Date initialDate = (Date) getArguments().getSerializable(MOVE_IN_DATE_KEY);
        int[] yearMonthDay = ymdTripleFor(initialDate);
        DatePickerDialog dialog = new DatePickerDialog(getActivity(), onDateSetListener, yearMonthDay[0], yearMonthDay[1],
                yearMonthDay[2]);
        return dialog;
    }

    private void setOnDateSetListener(OnDateSetListener listener) {
        this.onDateSetListener = listener;
    }

    private int[] ymdTripleFor(Date date) {
        Calendar cal = Calendar.getInstance(Locale.US);
        cal.setTime(date);
        return new int[]{cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)};
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以创建以下对话框片段之一:

DatePickerFragment.newInstance(yourDate, yourOnDateSetListener);
Run Code Online (Sandbox Code Playgroud)