我尝试了很多,但一切都不起作用。
我需要我的日期选择器,日期从2014年9月1日开始。但它从1900年开始。 我设定了最小日期概念,它在过去的日期都消失了。 请大家帮忙。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.daty);
click=(ImageView)findViewById(R.id.click);
hdate=(TextView)findViewById(R.id.hdate);
timeStamp = new SimpleDateFormat("dd/MM/yyyy").format(new Date(System.currentTimeMillis()));
hdate.setText(timeStamp);
final Calendar myCalendar = Calendar.getInstance();
final 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);
click.setVisibility(View.VISIBLE);
updateLabel();
}
private void updateLabel() {
// TODO Auto-generated method stub
String myFormat = "dd/MM/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
hdate.setText(sdf.format(myCalendar.getTime()));
}
};
hdate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DatePickerDialog datePickerDialog = new DatePickerDialog(Daty.this,date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
}
});
click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=new Intent(Daty.this,Mainy.class);
startActivity(in);
}
});
}
}
答案 0 :(得分:0)
您可以通过以下代码进行设置:
Calendar cal=Calendar.getInstance(Locale.ENGLISH);
datePickerDialog .init(cal.getTime().getYear()+1900, cal.getTime().getMonth(), cal.getTime().getDay(), this);
<强> 修改 强>
要为DatePicker
设置最短日期,您可以使用以下方式进行设置:
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
将今天的日期设为最低日期,所有过去日期均已停用。
注意:setMinDate是在API 11中引入的
编辑#2
您需要为此创建自定义DatePickerDialog
:
class DatePickerDialog1964 extends DatePickerDialog {
DatePickerDialog1964(Context c) {
super(c, null, 2014, 1, 1);
@SuppressWarnings("deprecation")
Date min = new Date(2014-1900, 1, 1);
DatePicker p = getDatePicker();
CalendarView cv = p.getCalendarView(); // should check for null
long cur = cv.getDate();
int d = cv.getFirstDayOfWeek();
p.setMinDate(min.getTime());
cv.setDate(cur + 1000L*60*60*24*40);
cv.setFirstDayOfWeek((d + 1) % 7);
cv.setDate(cur);
cv.setFirstDayOfWeek(d);
}
}
要使用它:
new DatePickerDialog1964(context).show();
答案 1 :(得分:0)
class DatePickerDialog1964 extends DatePickerDialog {
DatePickerDialog1964(Context c) {
super(c, null, 2013, 4, 21);
@SuppressWarnings("deprecation")
Date min = new Date(2013-1900, 4, 21);
DatePicker p = getDatePicker();
CalendarView cv = p.getCalendarView(); // should check for null
long cur = cv.getDate();
int d = cv.getFirstDayOfWeek();
p.setMinDate(min.getTime());
cv.setDate(cur + 1000L*60*60*24*40);
cv.setFirstDayOfWeek((d + 1) % 7);
cv.setDate(cur);
cv.setFirstDayOfWeek(d);
}
}
OR
DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();
Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMinDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;
答案 2 :(得分:0)
简单的解决方案:
int year = 2014;
int month = 1;
int day = 1;
DatePickerDialog datePickerDialog = new DatePickerDialog(EditProfileScreen.this,
android.R.style.Theme_Holo_Light_Dialog,
onDateSetListener, year, month, day);
datePickerDialog.setTitle(getString(R.string.birthday));
datePickerDialog.show();
因此日期选择器从2014年1月1日开始启动:)