3

我正在使用日期选择器来选择特定日期。日期选择器不应该允许选择星期天。直到现在我在选择后检测星期天并显示一条消息,但如果日期选择器不允许星期天会更好。有没有办法做到这一点?

检测星期天的代码:

 private static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
 {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
     [...]
    }

    public void onDateSet(DatePicker view, int year, int month, int day)
    {
        Calendar now = Calendar.getInstance();
        Calendar choosen = Calendar.getInstance();
        choosen.set(year, month, day);
        if (now.compareTo(choosen) < 0)
        {
            UserMessage.showMessage("The choosen date is in future", "Invalid date", getActivity());
            return;
        } else if (choosen.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
        {
            UserMessage.showMessage("The choosen date is on sunday!", "Invalid date",
                    getActivity());
            return;
        }
        [...]
    }
}         
4

1 回答 1

-1

我通过使用 datepicker 创建一个包含自定义 xml 布局的自定义对话框来解决这个要求。

我为每个人创建了以下示例,他们需要一个具有特殊需求的日期选择器对话框,例如没有星期天,没有更早的日期,可以选择特定日期:

创建自定义日期选择器:

public void showDatePicker() {
    final String dateFormatStr = "yyyyMMdd";
    final String dateSearchFormatStr = "yyyy-MM-dd";
    final String dateFormatStrGUI = "dd.MM.yyyy";
    final String dateFormatStrGUIDatepickerDialog = "EEEE, dd.MM.yyyy";
    final String minDateAvailable = "01.01.2005";
    // Initializiation
    LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    View customView = inflater.inflate(R.layout.activity_dateselect_dialog, null);
    dialogBuilder.setView(customView);
    final DatePicker datePicker = (DatePicker) customView.findViewById(R.id.activity_thumbnail_dateselect_dialog_datepicker);
    final TextView dateTextView = (TextView) customView.findViewById(R.id.activity_thumbnail_dateselect_dialog_dateview);
    final SimpleDateFormat dateTextViewFormatter = new SimpleDateFormat(Constants.dateFormatStrGUIDatepickerDialog, Locale.GERMANY);
    // Minimum date
    SimpleDateFormat formatter = new SimpleDateFormat(Constants.dateFormatStrGUI, Locale.GERMANY);
    Calendar minDate = Calendar.getInstance();
    try {
        minDate.setTime(formatter.parse(Constants.minDateAvailable));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    datePicker.setMinDate(minDate.getTimeInMillis());
    // View settings
    dialogBuilder.setTitle("Choose date");
    Calendar choosenDate = Calendar.getInstance();
    int year = choosenDate.get(Calendar.YEAR);
    int month = choosenDate.get(Calendar.MONTH);
    int day = choosenDate.get(Calendar.DAY_OF_MONTH);
    try {
        Date choosenDateFromUI = formatter.parse(newspaperDatePickerButton.getText().toString());
        choosenDate.setTime(choosenDateFromUI);
        year = choosenDate.get(Calendar.YEAR);
        month = choosenDate.get(Calendar.MONTH);
        day = choosenDate.get(Calendar.DAY_OF_MONTH);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Calendar dateToDisplay = Calendar.getInstance();
    dateToDisplay.set(year, month, day);
    dateTextView.setText(dateTextViewFormatter.format(dateToDisplay.getTime()));
    // Buttons
    dialogBuilder.setNegativeButton("today", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            newspaperDatePickerButton.setText(R.string.date_latest_identifier);
            newspaperDate = getString(R.string.date_latest_identifier);
            controller.loadNewspaper(newspaperDate, model.getSubscriptions().get(newspaperEditionPosition), true);
            dialog.dismiss();
        }
    });
    dialogBuilder.setPositiveButton("Apply", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            SimpleDateFormat formatter = new SimpleDateFormat(Constants.dateFormatStrGUI, Locale.GERMANY);
            Calendar now = Calendar.getInstance();
            Calendar choosen = Calendar.getInstance();
            choosen.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
            if (now.compareTo(choosen) == 0) {
                newspaperDate = getString(R.string.date_latest_identifier);
                newspaperDatePickerButton.setText(newspaperDate);
            } else {
                newspaperDate = formatter.format(choosen.getTime());
                newspaperDatePickerButton.setText(newspaperDate);
            }
            controller.loadNewspaper(newspaperDate, model.getSubscriptions().get(newspaperEditionPosition), false);
            dialog.dismiss();
        }
    });
    final AlertDialog dialog = dialogBuilder.create();
    // Datepicker
    datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
        public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar choosenDate = Calendar.getInstance();
            choosenDate.set(year, monthOfYear, dayOfMonth);
            Calendar now = Calendar.getInstance();
            dateTextView.setText(dateTextViewFormatter.format(choosenDate.getTime()));
            if (choosenDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || now.compareTo(choosenDate) < 0) {
                dateTextView.setTextColor(Color.parseColor("#ff0000"));
                ((Button) dialog.getButton(AlertDialog.BUTTON_POSITIVE)).setEnabled(false);
            } else {
                dateTextView.setTextColor(Color.parseColor("#000000"));
                ((Button) dialog.getButton(AlertDialog.BUTTON_POSITIVE)).setEnabled(true);
            }
        }
    });
    // Finish
    dialog.show();
}

布局

<TextView
        android:id="@+id/activity_thumbnail_dateselect_dialog_dateview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_margin="12dp"
        android:text=""
        android:layout_gravity="center"/>

<DatePicker
        android:id="@+id/activity_thumbnail_dateselect_dialog_datepicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:calendarViewShown="false"/>

于 2013-07-18T12:53:16.113 回答