在与另一个日期选择器相关的其他日期选择器上设置最小日期

Setting minimum date on other date picker relevant to another date picker

我知道以前可能有人问过这个问题,但我找不到解决我的问题的方法。我有两个 DatePickerDialogs,无论何时设置开始日期,我都会将另一个 datepickerdialog 的最小日期设置为该日期。但它仍然不起作用,因为结束日期选择器的最小日期与开始日期选择器相同。这是我的参考代码:

 Calendar c = Calendar.getInstance();
    selectedEndDay   = selectedStartDay   = c.get(Calendar.DAY_OF_MONTH);
    selectedEndMonth = selectedStartMonth = c.get(Calendar.MONTH);
    selectedEndYear  = selectedStartYear  = c.get(Calendar.YEAR);



    startDatePickerDialog =  new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            selectedStartYear = year;
            selectedStartMonth = monthOfYear;
            selectedStartDay = dayOfMonth;
            Log.e(TAG, "SET DATE: " + selectedStartYear + "/" + selectedStartMonth + "/" + selectedStartDay);

            btnStartDate.setText((selectedStartMonth + 1) + "/" + selectedStartDay + "/" + selectedStartYear);

            //set minimum date of end datepicker
            Calendar c2 = Calendar.getInstance();
            c2.set(year, monthOfYear, dayOfMonth);
            endDatePickerDialog.getDatePicker().setMinDate(c2.getTime().getTime());

        }
    },selectedStartYear,selectedStartMonth,selectedStartDay);

    startDatePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()-1000);

    endDatePickerDialog =  new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            selectedEndYear = year;
            selectedEndMonth = monthOfYear;
            selectedEndDay = dayOfMonth;
            Log.e(TAG, "SET DATE: " + selectedEndYear + "/" + selectedEndMonth + "/" + selectedEndDay);

            btnEndDate.setText((selectedEndMonth+1)+"/"+selectedEndDay+"/"+selectedEndYear);


        }
    },selectedEndYear,selectedEndMonth,selectedEndDay);


    //set minimum date of end datepicker
    endDatePickerDialog.getDatePicker().setMinDate(c.getTime().getTime());

我将在下面使用此代码进行演示。

这是 class 文件。

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.EditText;

public class Example extends Activity {

    private EditText startDate, endDate;
    private int selectedStartYear;

    private int selectedStartDay;
    private     int selectedStartMonth;
    private int selectedEndDay;
    private int selectedEndMonth;
    private int selectedEndYear;

    private     DatePickerDialog SecD, firstD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);



        startDate = (EditText) findViewById(R.id.editText1);
        endDate = (EditText) findViewById(R.id.editText2);


        final Calendar c = Calendar.getInstance();
        selectedEndDay = selectedStartDay = c.get(Calendar.DAY_OF_MONTH);
        selectedEndMonth = selectedStartMonth = c.get(Calendar.MONTH);
        selectedEndYear = selectedStartYear = c.get(Calendar.YEAR);
        SecD = new DatePickerDialog(Pdf.this, new SecondDate(),
                selectedEndYear, selectedEndMonth, selectedEndDay);

        firstD = new DatePickerDialog(Pdf.this, new FirstDate(),
                selectedStartYear, selectedStartMonth, selectedStartDay);
        startDate.setOnClickListener(new OnClickListener() {

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

                firstD.show();

            }
        });

        endDate.setOnClickListener(new OnClickListener() {

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

                SecD.show();

            }
        });

    }

    class FirstDate implements DatePickerDialog.OnDateSetListener {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // Doing thing with first Date pick Dialog

            selectedStartYear = year;
            selectedStartMonth = monthOfYear;
            selectedStartDay = dayOfMonth;

            startDate.setText((selectedStartMonth + 1) + "/" + selectedStartDay
                    + "/" + selectedStartYear);

            // set minimum date of end datepicker
            Calendar c2 = Calendar.getInstance();
            c2.set(year, monthOfYear, dayOfMonth);

            SecD.getDatePicker().setMinDate(c2.getTime().getTime());

        }

    }

    class SecondDate implements DatePickerDialog.OnDateSetListener {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            // Doing thing with second Date Picker Dialog

            selectedEndYear = year;
            selectedEndMonth = monthOfYear;
            selectedEndDay = dayOfMonth;

            endDate.setText((selectedEndMonth + 1) + "/" + selectedEndDay + "/"
                    + selectedEndYear);
        }

    }
}

这是 xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parentLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

</LinearLayout>