我的应用程序中有一个日期选择器。我想在点击日期按钮时应用几个条件,这样的条件是它应该禁用周末。我在stackoverflow中遇到了这个问题的答案,但它给出了一个错误。拜托,你能帮助我吗?提前谢谢
{
//Declaration for class
ButtonViews views;
dpListener dpListenerView;
// Declartion for member vairables
int day, month, x_year;
int hour;
int minute;
Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
views = new ButtonViews();
dpListenerView = new dpListener();
//ButtonListener
views.button_date.setOnClickListener(this);
views.button_time.setOnClickListener(this);
//
// pick up the default date using Calender class
calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
day = calendar.get(Calendar.DAY_OF_MONTH);
month = calendar.get(Calendar.MONTH);
x_year = calendar.get(Calendar.YEAR);
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
setupDate(day, month, x_year);
setupTime(hour, minute);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_date:
showDatePickerDialog();
break;
case R.id.button_time:
showTimePickerDialog();
break;
}
}
private void setupTime(int hours, int minutes) {
views.button_time.setText(hours + ":" + minutes);
}
private void setupDate(int day, int month, int year) {
String strMonth = ((month+1) <=9) ? ("0" + (month+1)) : String.valueOf(month+1);
views.button_date.setText(String.valueOf(day) + "/" + strMonth + "/" + String.valueOf(year));
}
private void showDatePickerDialog() {
DatePickerDialog datepickerdialog = new DatePickerDialog
(
this,
dpListenerView,
/* new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
} },*/
//this,
x_year,
month,
day
);
datepickerdialog.getDatePicker().setMaxDate(System.currentTimeMillis()+1000);
datepickerdialog.getDatePicker().setMinDate(System.currentTimeMillis()-10000);
datepickerdialog.show();
}
/* private OnDateSetListener dpListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
*//* day = dayOfMonth;
month = monthOfYear;
x_year = year;*//*
setupDate(dayOfMonth,monthOfYear,year);
}
};*/
public void showTimePickerDialog() {
TimePickerDialog timePickerDialog = new TimePickerDialog(
DateTimePickerActivity.this,
this,
hour,
minute,
true
);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minute);
timePickerDialog.show();
}
// @Override
// public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// setupDate(dayOfMonth,monthOfYear,year);
// }
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
setupTime(hourOfDay, minute);
}
class ButtonViews {
Button button_time;
Button button_date;
public ButtonViews() {
button_date = (Button) findViewById(R.id.button_date);
button_time = (Button) findViewById(R.id.button_time);
}
}
class dpListener implements OnDateSetListener
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
/* day = dayOfMonth;
month = monthOfYear;
x_year = year;*/
setupDate(dayOfMonth,monthOfYear,year);
}
}
答案 0 :(得分:1)
public DatePickerDialog (
Context context,
DatePickerDialog.OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth)
必须是year, month, day
, 不 day, month, year
。
至于你一小时外出的问题,我要看的第一个是默认时区。当时间超出一定的时间时,几乎总是与之相关。
这可能是因为您与默认设置处于不同的时区,或者您的夏令时配置不正确。
要弄清楚您的时区,请查看this question。
答案 1 :(得分:0)
试试这个:
private void showDatePickerDialog() {
DatePickerDialog datepickerdialog = null;
datepickerdialog = new DatePickerDialog
(
this,
/* new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
} },*/
this,
year,
month,
day
);
datepickerdialog.show();
}