在DatePickerDialog中实现OnClickListener来做某事不起作用

时间:2017-05-03 17:46:21

标签: android onclicklistener android-datepicker

我试图制作一个DatePickerDialog来更改TextView。但是当用户点击" Ok"时,我也希望显示Toast。按钮。

所以,我实现了DatePickerDialog.OnClickListenerDatePickerDialog.OnDateSetListener,我重写了这两种方法。但是当我点击" Ok" DatePicker上的按钮。有什么想法吗?

提前感谢,这是我的DatePickerFragment:

@RequiresApi(api = Build.VERSION_CODES.N)
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener, DatePickerDialog.OnClickListener {

    public static int year_x;
    public static int month_x;
    public static int day_x;
    public static Calendar calendar = Calendar.getInstance(Locale.FRANCE);


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance(Locale.FRANCE);
        final int year = c.get(Calendar.YEAR);
        final int month = c.get(Calendar.MONTH);
        final int day = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);

        /*
        // When I do that way and not implement DatePickerDialog.OnClickListener the date is not updated in my TextView switcherDateDay
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
            @SuppressLint("ShowToast")
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getActivity(), "Test", Toast.LENGTH_LONG).show();
            }
        });
        */
        return dialog;
    }


    @Override
    public void onClick(DialogInterface dialog, int which) {
        if(which == DialogInterface.BUTTON_POSITIVE) {
            Toast.makeText(getActivity(), "Date is selected", Toast.LENGTH_LONG).show();
        }
    }


    @TargetApi(Build.VERSION_CODES.N)
    @RequiresApi(api = Build.VERSION_CODES.M)
    public void onDateSet(DatePicker view, int year, int month, int day) {
        year_x  = view.getYear();
        month_x = view.getMonth();
        day_x = view.getDayOfMonth();

        calendar.set(year_x, month_x, day_x);
        ScheduleCalendar scheduleCal = new ScheduleCalendar(calendar, 0);

        TextView switcherDateDay = (TextView) getActivity().findViewById(R.id.schedule_switcher_date);
        TextView switcherCurrentDay = (TextView) getActivity().findViewById(R.id.schedule_switcher_current_day);
        switcherDateDay.setText(String.format("%s %s %s", scheduleCal.getDayName(), scheduleCal.getDayNo(), scheduleCal.getMonthName()));
        switcherCurrentDay.setText(ScheduleSwitcherFragment.statueOfWeek(scheduleCal));
    }
}

3 个答案:

答案 0 :(得分:1)

在datepicker对话框的onDateSet方法中显示Toast

答案 1 :(得分:0)

答案 2 :(得分:0)

如果你想这样做,我相信你需要实现DialogInterface.OnClickListener()(Not DatePickerDialog.OnClickListener())。虽然我不确定为什么你不会按照它在OnCreateDialog方法中注释掉的代码中所做的那样去做。

此外,您仍然需要将OnClickListener添加到您希望获取点击次数的按钮。