我使用Android的默认日期选择器和我分支持SDK是10,我想设置分和日期选择器的最大日期。
下面是我在我的MainActivity类的onCreate方法后:
私人无效registerButtonListeners(){
Calendar.setOnClickListener(新View.OnClickListener()
{
@覆盖
公共无效的onClick(视图v){
的ShowDialog(DATE_PICKER_DIALOG);
}
});
}
@覆盖
受保护的对话框onCreateDialog(INT ID)
{
开关(id)的
{
案例DATE_PICKER_DIALOG:返回showDatePicker();
}
返回super.onCreateDialog(ID);
}
私人DatePickerDialog showDatePicker()
{
DatePickerDialog日期选择器=新DatePickerDialog(MainActivity.this,android.R.style.Theme_Holo_Dialog_NoActionBar_MinWidth,新DatePickerDialog.OnDateSetListener(){
@覆盖
公共无效onDateSet(DatePicker的观点,年整型,诠释monthOfYear,
INT DAYOFMONTH){
mCalendar.set(Calendar.YEAR,一年);
mCalendar.set(Calendar.MONTH,monthOfYear);
mCalendar.set(Calendar.DAY_OF_MONTH,DAYOFMONTH);
updateDateText();
}
},mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH));
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB)
{
长D = 1356994800000L;
//datePicker.getDatePicker().setMinDate(d);
//datePicker.getDatePicker().setMaxDate(1546210800000L);
}
返回日期选择器;
}
解决方案
您可以从一个DatePickerDialog底层的DatePicker(通过简单地调用getDatePicker()),并利用设置其边界:
setMinDate(long为MinDate) setMaxDate(long的maxDate)当参数是毫秒的平常人数自1月1日,在默认时区1970 00:00:00。你还是要计算当然这些数值,但应该是容易做到的Calendar类:只是通过当前的日期,并从添加或substract X年。
I am using Android's default date picker and my min supported SDK is 10 and I want to set the min and the max dates of the date picker.
Here is what I have in my MainActivity class after the onCreate method:
private void registerButtonListeners() {
Calendar.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
showDialog(DATE_PICKER_DIALOG);
}
});
}
@Override
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case DATE_PICKER_DIALOG: return showDatePicker();
}
return super.onCreateDialog(id);
}
private DatePickerDialog showDatePicker()
{
DatePickerDialog datePicker= new DatePickerDialog(MainActivity.this,android.R.style.Theme_Holo_Dialog_NoActionBar_MinWidth,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mCalendar.set(Calendar.YEAR,year);
mCalendar.set(Calendar.MONTH,monthOfYear);
mCalendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
updateDateText();
}
},mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
long d=1356994800000L;
//datePicker.getDatePicker().setMinDate(d);
//datePicker.getDatePicker().setMaxDate(1546210800000L);
}
return datePicker;
}
解决方案
You can get the underlying DatePicker from a DatePickerDialog (by simply calling getDatePicker()) and set its bounds using:
setMinDate(long minDate) setMaxDate(long maxDate)Where the argument is the usual number of milliseconds since January 1, 1970 00:00:00 in the default time zone. You'll still have to calculate these values of course, but that should be trivial to do with the Calendar class: just pass current date and add or substract x years from that..
相关推荐
最新文章