Android 如何在指定日期之后在datepicker中设置日期

Android 如何在指定日期之后在datepicker中设置日期,android,datetime,datepickerdialog,Android,Datetime,Datepickerdialog,这是我的XML文件,名为create_event.XML <!-- Start Date --> <LinearLayout style="@style/create_event_exc_margin_bottom" android:layout_width="match_parent" android:layout_height="wrap_content"

这是我的XML文件,名为create_event.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>

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

我会将您的结束日期侦听器更改为以下内容

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)) {
                    // handle the error
                } else {
                end_date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                }

            }
    };

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

如果您注意到,我使用的是一个全局变量
startDate
。当它被选中时,您需要全局存储它

Calendar startDate = Calendar.getInstance(); // global variable

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.set(Calendar.YEAR, year);
                    startDate.set(Calendar.MONTH, monthOfYear);
                    startDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    start_date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
        }
    };

    new DatePickerDialog(getActivity(),
         mDateSetListener, start_year, start_month, start_day).show();
}  
其次,看看DateFormatter类,我相信它们会对您有用


干杯。

您也可以使用endDate全局变量,因为您可能希望防止startDate也在endDate之后。是的,它正在工作。。。。非常感谢您分享您的知识。。。。实际上,我并没有将startDate声明为全局变量。。。这就是它不起作用的原因。。。
Calendar startDate = Calendar.getInstance(); // global variable

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.set(Calendar.YEAR, year);
                    startDate.set(Calendar.MONTH, monthOfYear);
                    startDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    start_date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
        }
    };

    new DatePickerDialog(getActivity(),
         mDateSetListener, start_year, start_month, start_day).show();
}