看看這個
class MyDataPicker extends DatePickerDialog{
int year, monthOfYear, dayOfMonth;
final Calendar c;
Date d1;
public MyDataPicker(Context context, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth){
super(context, callBack, year, monthOfYear, dayOfMonth);
this.year=year; this.monthOfYear=monthOfYear; this.dayOfMonth=dayOfMonth;
d1= new Date(year, monthOfYear, dayOfMonth);
c = Calendar.getInstance();
// TODO Auto-generated constructor stub
}
public void onDateChanged(DatePicker datepicker, int year, int month, int day){
long timeinmilies = d1.getTime();
Date d = new Date(year, month,day);
long millis = d.getTime();
if(millis<=timeinmilies){
this.year = year; this.monthOfYear = month; this.dayOfMonth = day;
}
datepicker.updateDate(this.year, monthOfYear, dayOfMonth);
super.onDateChanged(datepicker,this.year, monthOfYear, dayOfMonth);
}
}
private DatePickerDialog.OnDateSetListener myDateSetListener
= new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
// set Date in EditText or textView Here
DOB.setText((monthOfYear+1)+"-"+dayOfMonth+"-"+year);
}
};
// button click to open datepicker
public void onClickPickDate(View view){
final Calendar c = Calendar.getInstance();
int myYear = c.get(Calendar.YEAR);
int myMonth = (c.get(Calendar.MONTH));
int myDay = c.get(Calendar.DAY_OF_MONTH);
MyDataPicker datePickerDialog = new MyDataPicker(this,myDateSetListener,myYear, myMonth, myDay);
datePickerDialog.show();
}
我想我錯了實例,編輯我的第一篇文章 – Tony 2012-04-02 18:26:53
你能證明你的代碼......在這裏你得到錯誤 ESP系。 – 2012-04-03 04:46:25
y,我不需要佈局中的簡單小部件,我需要通過按下按鈕打開的小部件。這會更改我的EditText字段。 我需要重複使用此功能在許多活動......都是這個:http://developer.android.com/resources/tutorials/views/hello-datepicker.html我需要在我的所有活動使用 – Tony 2012-04-03 15:54:22