单击“中性”按钮时,在DatePickerDialog中设置日期

时间:2016-05-07 12:35:43

标签: android

当我按下“中立”按钮时,我试图让DatePickerDialog打开TimePickerDialog,然而,似乎我试图找到答案时已经走到了尽头。

  • 首先,我考虑过投入OnClick听众,但我该如何获得约会?
  • 其次,如果我使用onDateSet中的datePickerDialog,如何检查是否已按下“中性”按钮?

非常感谢任何帮助。

public class DatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
int position;

@SuppressLint("ValidFragment")
public DatePickerDialogFragment(int position) {
    this.position = position;
}
public DatePickerDialogFragment() {
    this(-1);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Calendar calendar = Calendar.getInstance();
    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);
    datePickerDialog.setCanceledOnTouchOutside(false);
    datePickerDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Set Time", //What do I do here?);
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onCancel(DialogInterface dialog) {
    if (position != -1)
    new RecyclerAdapter(getActivity()).notifyItemChanged(position);
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    Log.d("We have liftoff!", "DPD Listener called!");
    Log.d("onDateSet method", "Date set for: "+ dayOfMonth + "/" + monthOfYear + "/" + year);

    if (position == -1) {
        ((MainActivity) getActivity()).setAlarm(year, monthOfYear, dayOfMonth);
        // Call method from other activity
    }
    else {
        ((MainActivity)getActivity()).setAlarm(position, year, monthOfYear, dayOfMonth);
    }
}
}

1 个答案:

答案 0 :(得分:0)

好吧,我之前应该想到这一点,但我所做的就是使用datePickerDialog.getDatePicker.getWhatever从DatePicker获取我的值,然后传递它。我是正式的白痴。