在 android 中,我试图在一页上使用 3 个日期选择器。但是监听器只获取了我最后更改的 datepicker 的值,它为所有 3 个分配了相同的值。
我应该在 date1、date5 和 date7 中得到 3 个不同的值。所以我可以将它保存在数据库中。
//set the current date as default date
date = (DatePicker) findViewById(R.id.datePicker1);
date4 = (DatePicker) findViewById(R.id.datePicker2);
date6 = (DatePicker) findViewById(R.id.datePicker3);
OnDateChangedListener one_dateListener,two_dateListener,three_dateListener;
date.init(date.getYear(), date.getMonth(), date.getDayOfMonth(),
one_dateListener= new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year1,
int monthOfYear, int dayOfMonth) {
day = dayOfMonth;
month = monthOfYear;
year = Integer.toString(year1);
}
});
date1 = pad(day)+"-"+padMonth(month)+"-"+year1;
System.out.println("Date 1"+date1);
date4 = (DatePicker) findViewById(R.id.datePicker2);
date4.init(date4.getYear(), date4.getMonth(), date4.getDayOfMonth(),
two_dateListener=new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year1,
int monthOfYear, int dayOfMonth) {
day = dayOfMonth;
month = monthOfYear;
year = Integer.toString(year1);
}
});
date5 = pad(date4.getDayOfMonth())+"-"+padMonth(date4.getMonth())+"-"+date4.getYear();
date6 = (DatePicker) findViewById(R.id.datePicker3);
date6.init(date6.getYear(), date6.getMonth(), date6.getDayOfMonth(),
three_dateListener= new OnDateChangedListener()
{
public void onDateChanged(DatePicker view, int year1,
int monthOfYear, int dayOfMonth) {
day = dayOfMonth;
month = monthOfYear;
year = Integer.toString(year1);
}
}
);
date7 = pad(date6.getDayOfMonth())+"-"+padMonth(date6.getMonth())+"-"+date6.getYear();