gpt4 book ai didi

java - Android DatePicker 对话框限制最小年份和最大年份

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

我一直在与我的部分代码作斗争一段时间,以对 datepickerdialog 设置年龄限制,但它不起作用,我已经研究过,但似乎没有一个解决方案起作用。这是到目前为止我的代码:

EditText birthdate=(EditText) findViewById(R.id.register_birth);
public void getdate() {
final Calendar myCalendar = Calendar.getInstance();

final DatePickerDialog.OnDateSetListener 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();
}

};
birthdate.setOnTouchListener(new View.OnTouchListener() {

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
new DatePickerDialog(Register.this, date, myCalendar
.get(Calendar.YEAR), myCalendar
.get(Calendar.MONTH), myCalendar
.get(Calendar.DAY_OF_MONTH)).show();

}
return true;
}
});

}

private void updateLabel() {

String myFormat = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
final Calendar myCalendar = Calendar.getInstance();

birthdate.setText(sdf.format(myCalendar.getTime()));
}

最佳答案

你的代码应该是这样的

 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
mPickDate = (Button) findViewById(R.id.pickDate);

mPickDate.setOnClickListener(this);

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

maxYear = mYear - 7;
maxMonth = mMonth;
maxDay = mDay;

minYear = mYear - 18;
minMonth = mMonth;
minDay = mDay;

// display the current date (this method is below)
updateDisplay(maxYear, maxMonth, maxDay);

}

private void updateDisplay(int year, int month, int day) {
// TODO Auto-generated method stub
mDateDisplay.setText(new StringBuilder()
// Month is 0 based so add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);

}

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

public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
/*
* mYear = year;
* mMonth = monthOfYear;
* mDay = dayOfMonth;
*/
// updateDisplay();



if (year > maxYear ||monthOfYear > maxMonth && year == maxYear||
dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth){

view.updateDate(maxYear, maxMonth, maxDay);
updateDisplay(maxYear, maxMonth, maxDay);

}
else if (year < minYear ||monthOfYear < minMonth && year == minYear||
dayOfMonth < minDay && year == minYear && monthOfYear == minMonth) {

view.updateDate(minYear, minMonth, minDay);
updateDisplay(minYear, minMonth, minDay);
}
else {

view.updateDate(year, monthOfYear, dayOfMonth);
updateDisplay(year, monthOfYear, dayOfMonth);
}

}
};

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

关于java - Android DatePicker 对话框限制最小年份和最大年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437639/

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