gpt4 book ai didi

没有星期天的Android DatePicker

转载 作者:太空狗 更新时间:2023-10-29 15:27:13 25 4
gpt4 key购买 nike

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

检测星期日的代码:

 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;
}
[...]
}
}

最佳答案

我通过使用日期选择器创建包含自定义 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"/>

关于没有星期天的Android DatePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15678202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com