0

我在一项活动中有两个日期选择器。一个日期选择器从当天开始设置 MinDate。另一个具有当天的 MinDate 和第一个选择的日期选择器的 MaxDate。它工作正常,但是当我再次更改第一个日期选择器时,第二个日期选择器的 MaxDate 没有改变。

更改日期选择器后如何更改 MaxDate?

public class EventCreator extends AppCompatActivity {
    Button btnDate;
    TextView txtViewDate;
    Button btnDaterRes;
    TextView txtViewDateRes;
    TextView testtxt;
    int year_x,month_x,day_x;
    int year_res_x,month_res_x,day_res_x;
    static final int Dialog_id = 0;
    static final int Dialog_res_id = 1;
    SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-YYYY");
    DatePickerDialog datePick;
    Calendar cal_max;
    DatePickerDialog datePickRes;
    long maxDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_creator);
        findViewById(R.id.txtResDatum);
        final EditText txtResUur = (EditText) findViewById(R.id.txtResUur);
         txtViewDate = (TextView) findViewById(R.id.txtViewDate);
         txtViewDateRes = (TextView) findViewById(R.id.txtViewDateRes);
        testtxt = (TextView) findViewById(R.id.testtxt);

        final Calendar cal = Calendar.getInstance();
        year_x  = year_res_x= cal.get(Calendar.YEAR);
        month_x = month_res_x= cal.get(Calendar.MONTH);
        day_x =  day_res_x = cal.get(Calendar.DAY_OF_MONTH);
        cal_max = Calendar.getInstance();

        showDialogOnClick();
    }

    public void showDialogOnClick(){
        btnDate = (Button) findViewById(R.id.btnDatePicker);
        btnDaterRes = (Button) findViewById(R.id.btnDatePickerRes);

        btnDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(Dialog_id);
            }
        });

        btnDaterRes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(Dialog_res_id);

            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        if(id == Dialog_id) {
            datePick = new DatePickerDialog(this, dpickerListner, year_x, month_x, day_x);
            datePick.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            return datePick;
        }
        if(id == Dialog_res_id){
            datePickRes = new DatePickerDialog(this, dpickerListner_res, year_res_x, month_res_x, day_res_x);     datePickRes.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);  
            return datePickRes;
        }else{
            return null;
        }
    }

因此,当我设置第一个日期选择器的日期时,我想更新第二个日期选择器的 MaxDate。

    private DatePickerDialog.OnDateSetListener dpickerListner = new DatePickerDialog.OnDateSetListener(){
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){
            year_x = year;
            month_x = monthOfYear + 1;
            day_x = dayOfMonth;
            String dateOfPicker = day_x + "/" + month_x + "/" + year_x;
            txtViewDate.setText(dateOfPicker);

            cal_max.set(year , monthOfYear, dayOfMonth);//Year,Mounth -1,Day
            maxDate = cal_max.getTimeInMillis();
            testtxt.setText(cal_max.getTime().toString());
            try {
                datePickRes.getDatePicker().setMaxDate(maxDate);
            }catch (Exception e){
                Log.d("Time: ", cal_max.toString());
            }
        }
    };

    private DatePickerDialog.OnDateSetListener dpickerListner_res = new DatePickerDialog.OnDateSetListener(){
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){
            year_res_x = year;
            month_res_x = monthOfYear + 1;
            day_res_x = dayOfMonth;
            String dateOfPicker = day_res_x + "/" + month_res_x+ "/" + year_res_x;
            txtViewDateRes.setText(dateOfPicker);

        }
    };
}
4

0 回答 0