gpt4 book ai didi

java - 如何在 Android DatePicker 中禁用以前的日期?

转载 作者:行者123 更新时间:2023-12-01 18:09:16 25 4
gpt4 key购买 nike

我想在 DatePicker 中禁用过去的日期,并仅显示当前和 future 的日期...我正在使用下面的代码,但这也会显示过去的日期,请建议所需的更改。

date_btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
mDay = calendar.get(Calendar.DAY_OF_MONTH);
mMonth = calendar.get(Calendar.MONTH);
mYear = calendar.get(Calendar.YEAR);

OnDateSetListener callBack = new OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar calender_check = Calendar.getInstance(Locale.getDefault());
calender_check.set(Calendar.YEAR, year);
calender_check.set(Calendar.MONTH, monthOfYear);
calender_check.set(Calendar.DAY_OF_MONTH, dayOfMonth);
SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy");
Date date2 =calender_check.getTime();


updatedate(sdf2.format(date2),originalFormat.format(date2));
Log.d("Date >>>>", ""+date2);

Log.d("sel_date", sdf2.format(date2));

date_btn.setText(sdf2.format(date2));
}
};
DatePickerDialog dialog = new DatePickerDialog(Class.this, callBack, mYear, mMonth, mDay);

dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_NEGATIVE) {
// Log.i(TAG, "Cancel");
//isCancelled = true;
dialog.dismiss();}
}
});
dialog.setCancelable(false);
dialog.show();

}
});

最佳答案

你也许可以尝试这样的代码:

DatePicker View ;
最终长时间 = System.currentTimeMillis() - 1;
//设置现在的最短时间
view.setMinDate(时间);

关于java - 如何在 Android DatePicker 中禁用以前的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34134764/

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