我是android的新手,并尝试将每个东西设置为将datepicker对话框的最大日期设置为当前日期。完成R& D但没有成功。请有人帮助我。提前致谢
以下是代码:
Calendar myCalendar;
DatePickerDialog.OnDateSetListener date;
dobET = (EditText) findViewById(R.id.dobET);
myCalendar = Calendar.getInstance();
date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
dobET.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(ChangeProfileActivity.this, date,
myCalendar.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar
.get(Calendar.DAY_OF_MONTH)).show();
}
});
答案 0 :(得分:1)
DatePickerDialog dialog = null;
Calendar myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
private void updateLabel()
{
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
dobET.setText(sdf.format(myCalendar.getTime()));
}
dialog = new DatePickerDialog(MainActivity.this, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
试试这会对你有帮助....
答案 1 :(得分:0)
使用此:
mDPDdialog.getDatePicker().setMaxDate(new Date().getTime());
修改强> 试试这个:
this.calendar = new GregorianCalendar();
this.datePicker = (DatePicker) findViewById(R.id.mDatePicker);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// (picker is a DatePicker)
this.datePicker.setMaxDate(this.calendar.getTimeInMillis());
} else {
final int maxYear = this.calendar.get(Calendar.YEAR);
final int maxMonth = this.calendar.get(Calendar.MONTH);
final int maxDay = this.calendar.get(Calendar.DAY_OF_MONTH);
this.datePicker.init(maxYear, maxMonth, maxDay,
new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year,
int month, int day) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, month, day);
if (newDate.after(calendar)) {
view.init(maxYear, maxMonth, maxDay, this);
}
}
});
}