我正在尝试创建一个包含最大和最小日期的datepicker对话框。我已完成以下操作:
package com.androidexample.datepicker;
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class DatePickerExample extends Activity {
private TextView Output;
private Button changeDate;
private int year;
private int month;
private int day;
DatePickerDialog dpd;
private static final int MILLIS_IN_SECOND = 1000;
private static final int SECONDS_IN_MINUTE = 60;
private static final int MINUTES_IN_HOUR = 60;
private static final int HOURS_IN_DAY = 24;
private static final int DAYS_IN_YEAR = 365;
static final int DATE_PICKER_ID = 1111;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Output = (TextView) findViewById(R.id.Output);
changeDate = (Button) findViewById(R.id.changeDate);
// Get current date by calender
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// Show current date
Output.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// Button listener to show date picker dialog
changeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// On button click show datepicker dialog
showDialog(DATE_PICKER_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID:
final long MILLISECONDS_IN_YEAR =
(long)MILLIS_IN_SECOND * SECONDS_IN_MINUTE * MINUTES_IN_HOUR
* HOURS_IN_DAY * DAYS_IN_YEAR;
Calendar now = Calendar.getInstance();
int current_year=now.get(Calendar.YEAR);
// open datepicker dialog.
// set date picker for current date
// add pickerListener listner to date picker
dpd=new DatePickerDialog(this, pickerListener, year, month,day);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
//Calendar.getInstance().get(Calendar.YEAR)-19
dpd.getDatePicker().setMaxDate((current_year-16-1970)*MILLISECONDS_IN_YEAR);
dpd.getDatePicker().setMinDate((current_year-100-1970)*MILLISECONDS_IN_YEAR);
}
return dpd;
//return new DatePickerDialog(this, pickerListener, year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
Output.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
}
清单文件中的min sdk设置为8.但setmaxdate和setmindate显示错误。所以我将最小sdk级别更改为11.但是我希望代码适用于API级别低于11的级别。如何我是否将API级别的最大和最小年份设置为小于11?
答案 0 :(得分:0)
在DatePickerDialog
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date minDate=new Date("Jan-01-01");
minDate.setYear(Calendar.getInstance().get(Calendar.YEAR)-3914);
Date maxDate=new Date("Jan-01-01");
maxDate.setYear(Calendar.getInstance().get(Calendar.YEAR)+6085);
dpd.getDatePicker().setMinDate(minDate.getTime());
dpd.getDatePicker().setMaxDate(maxDate.getTime());