我有一个Android应用程序 - 在其中一个活动中,我为用户提出DatePickerDialog.OnDateSetListener
选择日期。
例如,我想确保用户选择星期三为星期三的日期。 我的问题是如何检查所选日期的某些条件 - 如有必要,请提出“请选择其他日期”消息并允许用户再次选择。
这是代码, 所有这些代码都在OnCreate方法
上mCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// Here I want to check if the date is good.
// if its good - call GetPlayersList()
// if not - raise a message and let user choose again
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, monthOfYear);
mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
new GetPlayersList().execute(Utilities
.getAccessToken(AttendanceActivity.this));
}
};
new DatePickerDialog(AttendanceActivity.this, date,
mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH),
mCalendar.get(Calendar.DAY_OF_MONTH)).show();
有什么想法吗?提前谢谢
答案 0 :(得分:2)
您可以使用mCalendar.get(Calendar.DAY_OF_WEEK)
:
int dayOfWeek = mCalendar.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek == Calendar.WEDNESDAY) {
// it is a Wednesday
} else {
// it is not a Wednesday
}
修改强>
它看起来像那样:
private void showDialog() {
final Calendar calendar = Calendar.getInstance();
new DatePickerDialog(AttendanceActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek == Calendar.WEDNESDAY) {
// it is a Wednesday - do what you want
new GetPlayersList().execute(Utilities.getAccessToken(AttendanceActivity.this));
} else {
// it is not a Wednesday - show the dialog again
showDialog();
}
}
},
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)).show();
}