DialogFragment DatePickerDialog不返回更改日期

时间:2015-08-08 02:55:55

标签: android datepicker android-dialogfragment

我在片段中有datepicker dialogframgent,当我在 onDateSet()方法中记录日期时,我只从datepicker微调器获取原始日期而不是更改日期。我一直在关注http://developer.android.com/guide/topics/ui/controls/pickers.html的例子,因为我是android的新手。知道我做错了吗?

对话片段

public static class DobFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

        final Calendar calendar = Calendar.getInstance();
        DatePicker picker;

        protected DialogInterface.OnClickListener btn_ok_listener = new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                picker.clearFocus();
                onDateSet(picker, picker.getYear(), picker.getMonth(), picker.getDayOfMonth());
            }
        };

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            LayoutInflater inflater = getActivity().getLayoutInflater();
            View spinner = inflater.inflate(R.layout.date_spinner, null);

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

            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
            picker = datePickerDialog.getDatePicker();

            datePickerDialog.setCancelable(true);
            datePickerDialog.setCanceledOnTouchOutside(true);
            datePickerDialog.setView(spinner);
            datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Ok", btn_ok_listener);
            datePickerDialog.onDateChanged(picker,picker.getYear(),picker.getMonth(),picker.getDayOfMonth());


            datePickerDialog.setTitle("Birthday");

            return datePickerDialog;
        }

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            Log.v("dates",String.valueOf(picker.getYear()) +"-" + String.valueOf(year) +"-" + String.valueOf(calendar.getTime()));
//            Intent intent = new Intent();
//            intent.putExtra("result",formattedDate);
//            getTargetFragment().onActivityResult(getTargetRequestCode(), 1, intent);
        }
    }

显示对话框方法

public void showDobDialog() {
    FragmentManager fragManager = myContext.getFragmentManager();
    DialogFragment frag = new DobFragment();
    frag.setTargetFragment(this,1);
    frag.show(fragManager, "dob");
}

日期微调器布局

<?xml version="1.0" encoding="utf-8"?>
<DatePicker
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:datePickerMode="spinner"
    android:calendarViewShown="false">
</DatePicker>

从OnDateSet登录

2015-2015-Fri Aug 07 19:35:55 PDT 2015

我的DatePicker测试运行 - 原始日期2015年8月7日

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个

@Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState){

    final AtomicBoolean buttonPressed = new AtomicBoolean();
    final DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), null, yearOfBirth, monthOfYear, dayOfMonth) {
        @Override protected void onStop() {
            super.onStop();
            if (!buttonPressed.get()) {
                callback.dialogDismissed(DobFragment.this, false);
            }
        }
    };

    datePickerDialog.setTitle(dialogTitle);
    OnClickListener listener = new OnClickListener() {
        @Override public void onClick(DialogInterface dialog, int which) {
            buttonPressed.set(true);
            boolean okSelected = which == BUTTON_POSITIVE;
            callback.dialogDismissed(DobFragment.this, okSelected);
            if (okSelected) {
                DatePicker datePicker = datePickerDialog.getDatePicker();
                //DatePicker does not take the value in the picker as final value entered by the user, instead it take the index of the current value
                //The below line clear the focus on the date picker making it update all its indexes and delivering the right values when clicking Set
                //button.
                datePicker.clearFocus();
                callback.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
            }
        }
    };
    datePickerDialog.setButton(BUTTON_NEGATIVE, "Cancel", listener);
    datePickerDialog.setButton(BUTTON_POSITIVE, "Set", listener);
    return datePickerDialog;
}