如何在onDateSet()中的两个DatePickers之间切换?

时间:2018-10-27 16:02:37

标签: android datepicker

我有一个DialogFragment,它设置为处理两个不同的DatePickers。捆绑软件中的“ FilterNum”使我可以对两个DatePickers使用两个不同的setTitle()。

DialogFragment使用侦听器将所选日期返回到MainActivity中的OnDateSet()。我希望能够在OnDateSet中设置一个切换方法,这样,如果选择了DatePicker#1,则执行案例#1;如果选择了DatePicker#2,则执行案例#2。

MainActivity

...
DatePicker #1

beforeDueDateLine.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Bundle bundle = new Bundle();
            bundle.putInt("FilterNum",1);
            FilterDueDatesFragment filterFragment = new FilterDueDatesFragment();
            filterFragment.setArguments(bundle);
            filterFragment.show(getSupportFragmentManager(), "filterDatePicker");                
            dialogFilter.dismiss();
        }
    });

DatePicker #2

afterDueDateLine.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Bundle bundle = new Bundle();
            bundle.putInt("FilterNum",2);
            FilterDueDatesFragment filterFragment = new FilterDueDatesFragment();
            filterFragment.setArguments(bundle);
            filfilterFragment.show(getSupportFragmentManager(), "filterDatePicker");
            dialogFilter.dismiss();

FilterFragment extends DialogFragment
...  
static final int ON_OR_BEFOREFILTER = 1;
static final int ON_OR_AFTERFILTER= 2;
private int mChosenFilter;
int year = 0, month = 0, day = 0;

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

    Bundle bundle = this.getArguments();
    if (bundle != null) {
        mChosenFilter = bundle.getInt("FilterNum");
    }

    if (getActivity() != null) {
        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
    }

    DatePickerDialog.OnDateSetListener listener = (DatePickerDialog.OnDateSetListener) getActivity();

    DatePickerDialog pickerBefore;
    DatePickerDialog pickerAfter;       

    switch (mChosenFilter) {

        case ON_OR_BEFOREFILTER:
            pickerBefore = new DatePickerDialog(getActivity(), THEME_DEVICE_DEFAULT_DARK, listener, year, month, day);
            pickerBefore.setTitle("Select 'On or Before' due date");
            return pickerBefore;

        case ON_OR_AFTERFILTER:
            pickerAfter = new DatePickerDialog(getActivity(), THEME_DEVICE_DEFAULT_DARK, listener, year, month, day);
            pickerAfter.setTitle("Select 'On or After' due date");
            return pickerAfter;
        }
    return null;
    }

MainActivity OnDateSet()

    @Override
    public void   
        onDateSet(android.widget.DatePicker view, int year, int month, int day) {

            // this is where i need to set up different code 
            if (pickerBefore) {
                ...
            }
            if (pickerAfter) {
            }

1 个答案:

答案 0 :(得分:1)

首先从MainActivity中删除onDateSetListener,然后添加2个方法onDateSetPicker1(int年,int月,int日)和onDateSetPicker2(int年,int月,int日)。然后在对话框片段中使用以下代码:

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

    Bundle bundle = this.getArguments();
    if (bundle != null) {
        mChosenFilter = bundle.getInt("FilterNum");
    }

    if (getActivity() != null) {
        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
    }

    DatePickerDialog.OnDateSetListener listener1 = new DatePickerDialog.OnDateSetListener() {
        onDateSet(android.widget.DatePicker view, int year, int month, int day) {
            ((MainActivity)getActivity()).onDateSetPicker1(int year, int month, int day);
        }
    }
    DatePickerDialog.OnDateSetListener listener2 = new DatePickerDialog.OnDateSetListener() {
        onDateSet(android.widget.DatePicker view, int year, int month, int day) {
            ((MainActivity)getActivity()).onDateSetPicker2(int year, int month, int day);
        }
    }

    DatePickerDialog pickerBefore;
    DatePickerDialog pickerAfter;       

    switch (mChosenFilter) {

        case ON_OR_BEFOREFILTER:
            pickerBefore = new DatePickerDialog(getActivity(), THEME_DEVICE_DEFAULT_DARK, listener1, year, month, day);
            pickerBefore.setTitle("Select 'On or Before' due date");
            return pickerBefore;

        case ON_OR_AFTERFILTER:
            pickerAfter = new DatePickerDialog(getActivity(), THEME_DEVICE_DEFAULT_DARK, listener2, year, month, day);
            pickerAfter.setTitle("Select 'On or After' due date");
            return pickerAfter;
        }
    return null;
    }

因此,对话框中将有不同的侦听器,这些不同的侦听器将调用父活动的不同方法。