gpt4 book ai didi

java - 自定义日期选择器应禁用周末

转载 作者:行者123 更新时间:2023-12-01 11:06:39 25 4
gpt4 key购买 nike

我的应用程序中有一个日期选择器。我想在单击日期按钮时应用一些条件,首先这样的条件是它应该禁用周末。我在 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);
}

}

最佳答案

根据 docs for the constructor :

public DatePickerDialog (
Context context,
DatePickerDialog.OnDateSetListener callBack,
int year,
int monthOfYear,
int dayOfMonth)

必须是年、月、日不是日、月、年它。

至于您的时间超出一小时的问题,我首先要考虑的是默认时区。当时间到了某个确切的小时数时,这几乎总是与此有关。

这可能是因为您所在的时区与默认时区不同,或者您的夏令时配置不正确。

要了解您的时区,请查看 this question .

关于java - 自定义日期选择器应禁用周末,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32879809/

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