问题描述
在我的应用程序需要设置年龄限制的 7-18年。我想dispaly日期选取器与选择范围有限。
In my application I need to set the age limit between 7-18 years. I want to dispaly the date-picker with limited range of options.
我如何可以编辑同一任务为日期选取器与此code 的DatePicker
How can I edit for the same task into date-picker with this code DatePicker
我使用下面code,限制一年,但在+/-按钮被点击,一年可高于/低于限制的范围内变化。我希望该用户不应该能够增加或减少日期值
I am using below code to restrict year but when "+/-" button is clicked, year can be changed above/below restricted range. I want that user should not be able to increase or decrease date values.
return new DatePickerDialog(this,
mDateSetListener,
mYear-7, mMonth, mDay);
请帮我为一组生日7- 18岁之间的日期限制,有可能的话再与天或数月。
Please help me for the set the date limit of birthdate between 7- 18 years, and if possible then with day and months.
另外,我怎样才能设置最小年龄限制?
Also, How can I set min age limit?
感谢。
推荐答案
speanding那么多的时间后,我终于得到了解决。
After speanding so much time, finally I got the solution.
@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;
}
}
这篇关于如何使用日期选择器的Android设置最小 - 最大年龄限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!