我有两个相互连接的日期选择器,当我更改第一个选择器的日期时,第二个日期也会自动更改, 这两个选择器有一个监听器调用日期更改监听器,如下所示
public class birthDate extends Activity{
Calendar c = Calendar.getInstance();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH);
int currentDay = c.get(Calendar.DAY_OF_MONTH);
TextView birthDatetv;
DatePicker birthDayDatePicker,periodDatePicker;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.birthdate);
birthDatetv = (TextView)findViewById(R.id.textViewDateBirth);
birthDayDatePicker = (DatePicker)findViewById(R.id.DatePickerBirthDay);
periodDatePicker = (DatePicker)findViewById(R.id.DatePickerPeriod);
periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
{
@Override
public void onDateChanged(DatePicker periodDatePicker, int currentYear, int currentMonth,int currentDay) {
// TODO Auto-generated method stub
birthDateCalculations();
}
});
birthDayDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener () {
@Override
public void onDateChanged(DatePicker birthDayDatePicker, int currentYear, int currentMonth, int currentDay) {
// TODO Auto-generated method stub
periodDateCalculations();
}
});
}
正如我之前提到的,拾取器是连接的,所以当我改变一个选择器日期时另一个也被改变,所以两个日期选择器的两个听众将被调用,我只想要与选择器有关的监听器用户更改了日期以进行调用,不知道该怎么做?
以下是听众可能提供帮助的方法
public void birthDateCalculations ()
{
Calendar start = Calendar.getInstance();
int periodYear = periodDatePicker.getYear();
int periodMonth = periodDatePicker.getMonth();
int periodDay = periodDatePicker.getDayOfMonth();
start.set(periodYear, periodMonth, periodDay);
birthDayDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
periodDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
Date periodDate = start.getTime();
int daysToAdd = 280;
Calendar cal = Calendar.getInstance();
cal.setTime(periodDate);
cal.add(Calendar.DAY_OF_MONTH,daysToAdd );
System.err.println("-----" +cal.getTime());
int birthYearAfterCalc = cal.getTime().getYear()+1900;
System.err.println("birthYearAfterCalc-----" + birthYearAfterCalc);
int birthMonthAfterCalc = cal.getTime().getMonth();
System.err.println("birthMonthAfterCalc----" + birthMonthAfterCalc);
int birthDayAfterCalc = cal.getTime().getDate();
System.err.println("birthDayAfterCalc"+birthDayAfterCalc);
//user edit period to get birth
if( periodDay <= currentDay && periodMonth <= currentMonth && periodYear <= currentYear){
//the program runs normally
birthDayDatePicker.updateDate(birthYearAfterCalc, birthMonthAfterCalc, birthDayAfterCalc);
}
else {
new AlertDialog.Builder(birthDate.this)
.setTitle("Wrong Data Input!")
.setMessage("Error in period date input")
.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
periodDatePicker.updateDate(currentYear,currentMonth, currentDay);
birthDayDatePicker.updateDate(currentYear, currentMonth, currentDay);
}
}
public void periodDateCalculations ()
{
Calendar start2 = Calendar.getInstance();
// get the chosen date from birth date picker
int birthYear = birthDayDatePicker.getYear();
int birthMonth = birthDayDatePicker.getMonth();
int birthDay = birthDayDatePicker.getDayOfMonth();
//set the chosen date to calendar instance
start2.set(birthYear, birthMonth, birthDay);
birthDayDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
periodDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
//get the date
Date birthDate = start2.getTime();
//int constrain = currentMonth + 9;
int daysToAdd = -280;
//another instance from calendar
Calendar cal2 = Calendar.getInstance();
//set birth date chosen to calendar
cal2.setTime(birthDate);
//add -280 day to birth day to get period date
cal2.add(Calendar.DAY_OF_MONTH,daysToAdd);
System.err.println("-----" +cal2.getTime());
int periodYearAfterCalc = cal2.getTime().getYear()+1900;
System.err.println("periodYearAfterCalc-----" + periodYearAfterCalc);
int periodMonthAfterCalc = cal2.getTime().getMonth();
System.err.println("periodMonthAfterCalc----" + periodMonthAfterCalc);
int periodDayAfterCalc = cal2.getTime().getDate();
System.err.println("periodDayAfterCalc"+periodDayAfterCalc);
//edit birth date picker from user
if(birthDay >= currentDay && birthMonth >= currentMonth && birthYear >= currentYear){
//the program runs normally
periodDatePicker.updateDate(periodYearAfterCalc, periodMonthAfterCalc, periodDayAfterCalc);
}
else{
new AlertDialog.Builder(birthDate.this)
.setTitle("Wrong Data Input!")
.setMessage("birth day input error")
.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
birthDayDatePicker.updateDate(currentYear,currentMonth, currentDay);
periodDatePicker.updateDate(currentYear, currentMonth, currentDay);
}
}
}
答案 0 :(得分:1)
您可以做的是在另一个采取操作时将相应的侦听器设置为null,然后在任务之后,您可以将其重新打开。
例如:
periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
{
@Override
public void onDateChanged(DatePicker periodDatePicker, int currentYear, int currentMonth,int currentDay) {
// TODO Auto-generated method stub
birthDayDatePicker.init(currentYear, currentMonth, currentYear, null);
birthDateCalculations();
restartBirthDayDatePickerListener();
// ^here you turn back on that init command that we have just set to null.
}
});
不是一个迷人的解决方案,但也许你可以做出正面或反面。