1

我有两个相互连接的日期选择器,因为当我更改第一个选择器的日期时,第二个选择器的日期也会自动更改,这两个选择器有一个称为日期更改侦听器的侦听器,如下所示

  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); 


                        }

    }

}
4

1 回答 1

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.
    }
});

不是一个迷人的解决方案,但也许你可以做出正面或反面。

于 2012-11-25T11:24:49.707 回答