在日期选择器对话框中设置最小日期会在蜂窝上方的版本上强制关闭,并在蜂窝下面的版本上正常工作

时间:2014-08-22 05:06:12

标签: android android-3.0-honeycomb android-2.3-gingerbread datepickerdialog

我的片段中有2个datepicker对话框。单击按钮时日期选择器激活。我有2个按钮1用于设置离开日期,另一个用于设置返回日期。单击“离开”按钮弹出一个日期选择器对话框,然后单击“返回”按钮弹出第二个datepicker对话框。在离开日期选择器对话框中,我需要禁用用户尝试选择小于今天日期的日期。在返回日期选择器对话框中,必须检查两个条件。首先,如果用户未从离开日期选择器对话框中选择离开日期,则返回日期选择器对话框应将日期显示为当前日期。当用户尝试选择小于今天的日期时,我需要禁止用户这样做。其次,如果用户使用出发日期选择器对话框设置了出发日期,则返回日期选择器对话框应显示与出发日期相同的日期。当用户试图选择离开日期以下的日期时,应该尝试将其停止。我已经在我的应用程序中成功实现了所有这些姜面包及以下版本。但问题是,当我尝试从蜂窝上面的版本实现这个时,应用程序就是强制关闭。如何在蜂窝以上版本的日期选择器对话框中设置最小日期。此外,我的应用程序适用于从froyo到kitkat的所有版本,因此我将min sdk版本设置为froyo的版本。我需要一个代码,它应该适用于蜂窝上下的两个版本。请帮帮我。这是我的代码。

 private int year;
    private int month;
    private int day;
    Boolean greaterthanhoneycomb=false;
    int departureday,departuremonth,departureyear;
    DatePickerDialog.OnDateSetListener datePickerListener,datepicker;


final Button departuredate=(Button)rootView.findViewById(R.id.button1);
    departuredate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            final Calendar c = Calendar.getInstance();
            year  = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH);
            day   = c.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(getActivity(), datePickerListener, 
                    year, month,day){



                        @Override
                        public void onDateChanged(DatePicker view,
                                int year, int month, int day) {
                            // TODO Auto-generated method stub

                            final Calendar c = Calendar.getInstance();
                           int mYear = c.get(Calendar.YEAR);
                           int mMonth = c.get(Calendar.MONTH);
                          int  mDay = c.get(Calendar.DAY_OF_MONTH);
                            //* this validation allow user to enter date less then current date *
                            if (year < mYear)
                                view.updateDate(mYear, mMonth, mDay);


                        if (month < mMonth && year == mYear)
                            view.updateDate(mYear, mMonth, mDay);

                        if (day < mDay && year == mYear && month == mMonth)
                            view.updateDate(mYear, mMonth, mDay);
                        super.onDateChanged(view, year, month, day);
                        }


            };

dialog.show();


        }
    });
    final Button returndate=(Button)rootView.findViewById(R.id.button2);
    returndate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(departureday==0)
            {
                 final Calendar c = Calendar.getInstance();
                    year  = c.get(Calendar.YEAR);
                    month = c.get(Calendar.MONTH);
                    day   = c.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog dialog = new DatePickerDialog(getActivity(), datepicker, year, month, day){

                    @Override
                    public void onDateChanged(DatePicker view, int year,
                            int month, int day) {
                        // TODO Auto-generated method stub
                        final Calendar c = Calendar.getInstance();
                           int mYear = c.get(Calendar.YEAR);
                           int mMonth = c.get(Calendar.MONTH);
                          int  mDay = c.get(Calendar.DAY_OF_MONTH);
                          //* this validation allow user to enter date less then current date *
                            if (year < mYear)
                                view.updateDate(mYear, mMonth, mDay);


                        if (month < mMonth && year == mYear)
                            view.updateDate(mYear, mMonth, mDay);

                        if (day < mDay && year == mYear && month == mMonth)
                            view.updateDate(mYear, mMonth, mDay);
                        super.onDateChanged(view, year, month, day);
                    }

                };
                dialog.show();
            }
            else
            {
            DatePickerDialog dialog = new DatePickerDialog(getActivity(), datepicker, departureyear, departuremonth, departureday){

                @Override
                public void onDateChanged(DatePicker view, int year,
                        int month, int day) {
                    // TODO Auto-generated method stub

                       int mYear =departureyear;
                       int mMonth = departuremonth;
                      int  mDay = departureday;
                        //* this validation allow user to enter date less then current date *
                        if (year < mYear)
                            view.updateDate(mYear, mMonth, mDay);


                    if (month < mMonth && year == mYear)
                        view.updateDate(mYear, mMonth, mDay);

                    if (day < mDay && year == mYear && month == mMonth)
                        view.updateDate(mYear, mMonth, mDay);
                    super.onDateChanged(view, year, month, day);
                }

            };
            dialog.show();
            }
        }
    });

    datepicker=new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // TODO Auto-generated method stub
            String date=dayOfMonth+"-"+(monthOfYear+1)+"-"+year;
            returndate.setText(" "+date);
        }
    };
    datePickerListener 
    = new DatePickerDialog.OnDateSetListener() {

// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
    int selectedMonth, int selectedDay) {
    //Do whatever you want
    departureday=selectedDay;
    departuremonth=selectedMonth;
    departureyear=selectedYear;
    String date=selectedDay+"-"+(selectedMonth+1)+"-"+selectedYear;
    departuredate.setText(" "+date);
}

};

0 个答案:

没有答案