gpt4 book ai didi

android - 如何不允许用户在日期选择器中选择过去的日期?

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:19 25 4
gpt4 key购买 nike

我想当用户在日期选择器中选择过去的日期时,它不应该接受昨天的日期。我的应用程序将提示消息通知用户不能选择小于当前日期。有人指导我吗?...谢谢

mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
Button pickDate_btn = (Button) findViewById(R.id.pickDate);
pickDate_btn.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});

final Calendar now = Calendar.getInstance();
mYear = now.get(Calendar.YEAR);
mMonth = now.get(Calendar.MONTH);
mDay = now.get(Calendar.DAY_OF_MONTH);

updateDisplay();

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {

case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
}

private void updateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("-")
.append(mMonth + 1).append("-")
.append(mYear).append(" "));
}

private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub

mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();

//should add code here?
}
};

最佳答案

从 API 级别 11 开始有一个方法:

DatePicker.setMinDate(new Date().getTime())

或者如果它不起作用(旧 API),那么,

Date datePickerDate = // get the value here;
Date currentDate = new Date();
if (datePickerDate.before(currentDate)) {
// error !
}

关于android - 如何不允许用户在日期选择器中选择过去的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9494334/

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