用户不应该在今天的日期安卓之前选择日期

时间:2014-04-03 06:47:38

标签: android datepickerdialog

这是我的名为create_event.xml的XML文件

<!--  Start Date  -->
          <LinearLayout
            style="@style/create_event_exc_margin_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                style="@style/create_event_exc_infotext_bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/start_date" />
            </LinearLayout>

             <LinearLayout
                 style="@style/create_event_exc_margin_bottom"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:orientation="horizontal" >
                <EditText 
                    style="@style/create_event_infotext_normal"
                    android:textColor="@android:color/black"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content" 
                     android:layout_weight="1"
                     android:background="@drawable/extra_desc"
                     android:id="@+id/start_date">
                </EditText>

                <Button 
                    android:layout_height="wrap_content"
                    android:layout_weight="0" 
                    android:id="@+id/btn_start_calender"
                    android:text="Calendar"
                    android:layout_width="100dp">
                </Button> 

             </LinearLayout>

        <!--  Start Time  -->      
               <LinearLayout
            style="@style/create_event_exc_margin_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                style="@style/create_event_exc_infotext_bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/start_time" />
            </LinearLayout>

             <LinearLayout
                 style="@style/create_event_exc_margin_bottom"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:orientation="horizontal" >

                 <EditText 
                     style="@style/create_event_infotext_normal"
                    android:textColor="@android:color/black"
                     android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_weight="1"
                android:background="@drawable/extra_desc"
            android:id="@+id/start_time">

        </EditText>

    <Button 
        android:layout_height="wrap_content"
        android:layout_weight="0" 
        android:id="@+id/btn_start_timepicker"
        android:text="Time Picker" 
        android:layout_width="100dp"></Button>

                 </LinearLayout>

              <!--  End Date  --> 
               <LinearLayout
            style="@style/create_event_exc_margin_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                style="@style/create_event_exc_infotext_bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/end_date" />
            </LinearLayout>

             <LinearLayout
                 style="@style/create_event_exc_margin_bottom"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:orientation="horizontal" >

                 <EditText 
                     style="@style/create_event_infotext_normal"
                    android:textColor="@android:color/black"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content" 
                     android:layout_weight="1"
                     android:background="@drawable/extra_desc"
                     android:id="@+id/end_date">
                </EditText>
                <Button 
                    android:layout_height="wrap_content"
                    android:layout_weight="0" 
                    android:id="@+id/btn_end_calender" 
                    android:text="Calendar"
                    android:layout_width="100dp">
                </Button> 
             </LinearLayout>

              <!--  End Time  --> 
                <LinearLayout
            style="@style/create_event_exc_margin_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                style="@style/create_event_exc_infotext_bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/end_time" />
            </LinearLayout>

             <LinearLayout
                 style="@style/create_event_exc_margin_bottom"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:orientation="horizontal" >

                 <EditText 
                     style="@style/create_event_infotext_normal"
                    android:textColor="@android:color/black"
                     android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:layout_weight="1"
                android:background="@drawable/extra_desc"
            android:id="@+id/end_time">

        </EditText>

    <Button 
        android:layout_height="wrap_content"
        android:layout_weight="0" 
        android:id="@+id/btn_end_timepicker"
        android:text="Time Picker" 
        android:layout_width="100dp"></Button>

                 </LinearLayout>

这是我的java文件......

 public class Create_Event extends SherlockFragment implements OnClickListener {

Button b_start_calender, b_end_calender, b_start_timepicker, b_end_timepicker;
EditText start_date, start_time, end_date, end_time;
private int start_year, start_month, start_day, start_hour, start_minute;
private int end_year, end_month, end_day, end_hour, end_minute;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

b_start_calender=(Button)rootView.findViewById(R.id.btn_start_calender);
    b_start_timepicker=(Button)rootView.findViewById(R.id.btn_start_timepicker);
    b_end_calender=(Button)rootView.findViewById(R.id.btn_end_calender);
    b_end_timepicker=(Button)rootView.findViewById(R.id.btn_end_timepicker);

    start_date=(EditText)rootView.findViewById(R.id.start_date);
    start_time=(EditText)rootView.findViewById(R.id.start_time);
    end_date=(EditText)rootView.findViewById(R.id.end_date);
    end_time=(EditText)rootView.findViewById(R.id.end_time);

    b_start_calender.setOnClickListener(this);
    b_start_timepicker.setOnClickListener(this);
    b_end_calender.setOnClickListener(this);
    b_end_timepicker.setOnClickListener(this);

return rootView;        
}


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


if(v==b_start_calender) {
    final Calendar c=Calendar.getInstance();
    start_year=c.get(Calendar.YEAR);
    start_month=c.get(Calendar.MONTH);
    start_day=c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year,
                int monthOfYear, int dayOfMonth) {
startDate = Calendar.getInstance();
            startDate.set(Calendar.YEAR, year);
            startDate.set(Calendar.MONTH, monthOfYear);
            startDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
           start_date.setText(dayOfMonth + "-"
                    + (monthOfYear + 1) + "-" + year);
        }
    };
    DatePickerDialog d = new DatePickerDialog(getActivity(),
             mDateSetListener, start_year, start_month, start_day);
    d.show();
}  

if(v==b_start_timepicker) {
    final Calendar c = Calendar.getInstance();
    start_hour=c.get(Calendar.HOUR_OF_DAY);
    start_minute=c.get(Calendar.MINUTE);

    TimePickerDialog.OnTimeSetListener mTimeSetListner = new TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            start_time.setText(hourOfDay + ":" + minute);
        }
    };
    TimePickerDialog t=new TimePickerDialog(getActivity(), mTimeSetListner, start_hour, start_minute,false);
    t.show();
}

if(v==b_end_calender) {
    final Calendar c=Calendar.getInstance();
    end_year=c.get(Calendar.YEAR);
    end_month=c.get(Calendar.MONTH);
    end_day=c.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()  {
        public void onDateSet(DatePicker view, int year,
                int monthOfYear, int dayOfMonth)  {
Calendar newEndDate=Calendar.getInstance();
            newEndDate.set(Calendar.YEAR, year);
            newEndDate.set(Calendar.MONTH, monthOfYear);
            newEndDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);



            if(newEndDate.before(startDate))
            {
                end_date.setText("Plz.. enter date after starting date..");

            }

            else
            {

                end_date.setText(dayOfMonth + "-"
                        + (monthOfYear + 1) + "-" + year);

            }


        }
    };
    DatePickerDialog d = new DatePickerDialog(getActivity(),
             mDateSetListener, end_year, end_month, end_day);
    d.show();       
}  


if(v==b_end_timepicker) {
    final Calendar c = Calendar.getInstance();
    end_hour=c.get(Calendar.HOUR_OF_DAY);
    end_minute=c.get(Calendar.MINUTE);

    TimePickerDialog.OnTimeSetListener mTimeSetListner = new TimePickerDialog.OnTimeSetListener()  {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            end_time.setText(hourOfDay + ":" + minute);
        }
    };
    TimePickerDialog t=new TimePickerDialog(getActivity(), mTimeSetListner, end_hour, end_minute,false);
    t.show();
}
}

此处,可以正确设置开始日期和结束日期。在开始日期之前无法设置结束日期。但是,如果我们先设置结束日期然后再开始日期...在这种情况下,它无法正常工作。如何处理这个案子???我还需要开始和结束日期不能相同,用户无法在今天的日期之前选择日期...在哪里放条件?我怎样才能做到这一点??请帮忙......

1 个答案:

答案 0 :(得分:0)

从API级别11开始,有一种方法: date.before(today)date.equals(today)

使用DatePicker.setMaxDate(long maxDate)设置最长日期,DatePicker.setMinDate(long maxDate)设置Datepicker的最短日期。

在您的情况下,diable结束日期,直到选择开始日期。

if(startDate.before(today) || startDate.equals(today)){
   //error !
}

if(startDate.equals(EndDate)){
   //error !
}
相关问题