从android日期选择器返回日期到调用方法

时间:2016-02-24 20:25:25

标签: android android-datepicker

我正在遍历日期条目列表。如果日期条目不是有效日期,我调用DatePickerDialog。我不明白的是,我如何将所选日期返回到调用方法,以便我可以使用它

呼叫功能:

else {
            Log.i(TAG, "Not a valid date");
            // get actual date
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_WEEK);

            // open date picker
            DatePickerDialog datePickerDialog = new DatePickerDialog(this, this, year, month, day);
            String dateString;
            datePickerDialog.setMessage("Wrong date: " + helpList.get(0));
            datePickerDialog.show();
            // use chosen date
            if (chosenDate.length() > 0) {
                Log.i(TAG, "Date will be set: " + chosenDate);
            }
        }

onDateSet方法:

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

    Calendar aCalendar = Calendar.getInstance();
    aCalendar.set(year, monthOfYear, dayOfMonth);
    SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
    String theDate = formatter.format(aCalendar.getTime());
    Log.i(TAG, "Chosen date formatet: " + theDate);
    chosenDate = theDate;
}

1 个答案:

答案 0 :(得分:0)

您可以创建DialogFragment并在您的活动中使用它,如下所示:

   DialogFragment newFragment = new DatePickerFragment();
   newFragment.show(getSupportFragmentManager(), "datePicker");

DatePickerFragment应该扩展DialogFragment,看起来像这样:

public class DatePickerFragment extends DialogFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), dpickerListener, year, month, day);
    }

    private DatePickerDialog.OnDateSetListener dpickerListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            EditText dateEditText = (EditText) getActivity().findViewById(getArguments().getInt("editTextId"));
            GregorianCalendar cal = new GregorianCalendar();
            cal.set(year, monthOfYear, dayOfMonth);
            SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
            fmt.setCalendar(cal);
            String dateFormatted = fmt.format(cal.getTime());
            dateEditText.setText(dateFormatted);
            dateEditText.clearFocus();
        }
    };
}

注意如何通过以下行访问活动:

EditText dateEditText = (EditText) getActivity().findViewById(getArguments().getInt("editTextId"));

这样您就可以在调用活动中实现一些逻辑。当然,您可以在调用活动中调用一个方法 - 如下所示:

((MyActivity)getActivity()).doSomethingWithData(dateFormatted);