如何在Android日期选择器中将特定选择的日期设置为最小日期?(java) [英] How can I set the specific selected date to min date in android date picker? (java)

查看:63
本文介绍了如何在Android日期选择器中将特定选择的日期设置为最小日期?(java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个DatePicker,一个是开始日期选择器,另一个是结束日期选择器.我要将结束DatePicker的最小日期设置为用户选择的开始日期.

我设置了用户在Calander startDate中选择的数据,并在结束日期将此数据设置为MinDate.我已经尝试过这种方法,但是它不起作用:(

我不能使用Mateiral Range Date Picker,因为我必须使用特定的主题.另外,我也不擅长编程,因此,如果您能详细解释它,我将不胜感激.

  ipDcEventStartDay.setOnClickListener(new View.OnClickListener(){@Override公共无效onClick(View v){最终的日历c = Calendar.getInstance();startYear = c.get(Calendar.YEAR);startMonth = c.get(Calendar.MONTH);startDay = c.get(Calendar.DAY_OF_MONTH);DatePickerDialog dpd =新的DatePickerDialog(DoubleCheckEventActivity.this,新的DatePickerDialog.OnDateSetListener(){@Overridepublic void onDateSet(DatePicker view,int year,int month,int dayOfMonth){startYear =年;startMonth = month + 1;startDay = dayOfMonth;ipDcEventStartDay.setText(startYear +``-''+ startMonth +``-''+ startDay);ipDcEventStartDay.setTextColor(Color.parseColor(#000000")));}},startYear,startMonth,startDay);dpd.show();}});日历startDate = Calendar.getInstance();startDate.set(Calendar.YEAR,startYear);startDate.set(Calendar.MONTH,startMonth);startDate.set(Calendar.DAY_OF_MONTH,startDay);ipDcEventEndDay.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){最终的日历c = Calendar.getInstance();endYear = c.get(Calendar.YEAR);endMonth = c.get(Calendar.MONTH);endDay = c.get(Calendar.DAY_OF_MONTH);DatePickerDialog dpd =新的DatePickerDialog(DoubleCheckEventActivity.this,新的DatePickerDialog.OnDateSetListener(){@Overridepublic void onDateSet(DatePicker view,int year,int month,int dayOfMonth){endYear =年;endMonth =月;endDay = dayOfMonth;ipDcEventEndDay.setText(endYear +"-" +(endMonth + 1)+"-" endDay);ipDcEventEndDay.setTextColor(Color.parseColor(#000000")));}},endYear,endMonth,endDay);dpd.getDatePicker().setMinDate(startDate.getTimeInMillis());dpd.show();}}); 

解决方案

此代码:

  startDate.set(Calendar.YEAR,startYear);startDate.set(Calendar.MONTH,startMonth);startDate.set(Calendar.DAY_OF_MONTH,startDay); 

应该在里面

  ipDcEventEndDay.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){} 

因为 startYear startMonth startDay 仅在 ipDcEventStartDay.setOnClickListener()上设置,所以在开始时它们具有一些值,但没有在 ipDcEventStartDay.setOnClickListener()

上设置的值.

因此,在 ipDcEventEndDay.setOnClickListener()中,您必须在 startDate 上再次设置在其他 DatePickerDialog

I have 2 DatePickers, one is start date picker and the other is end date picker. I want to set the min date of the end DatePicker to the start date which user has selected.

I set the Data which user selected in Calander startDate and set this data as MinDate at end date. I've tried in this way but it's not working :(

I can't use Mateiral Range Date Picker because I have to use specific theme. Also I'm not good at programming so I'd appreciate it if you could explain it in detail.

ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v){
        final Calendar c = Calendar.getInstance();

        startYear = c.get(Calendar.YEAR);
        startMonth = c.get(Calendar.MONTH);
        startDay = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                startYear = year;
                startMonth = month+1;
                startDay = dayOfMonth;

                ipDcEventStartDay.setText(startYear + " - " + startMonth + " - " + startDay);
                ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
            }
        }, startYear, startMonth, startDay);
        dpd.show();
    }
});

Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);

ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final Calendar c = Calendar.getInstance();

        endYear = c.get(Calendar.YEAR);
        endMonth = c.get(Calendar.MONTH);
        endDay = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                endYear = year;
                endMonth = month;
                endDay = dayOfMonth;

                ipDcEventEndDay.setText(endYear + " - " + (endMonth+1) + " - " + endDay);
                ipDcEventEndDay.setTextColor(Color.parseColor("#000000"));
            }
        }, endYear,endMonth, endDay);
        dpd.getDatePicker().setMinDate(startDate.getTimeInMillis());
        dpd.show();
    }
});

解决方案

This code:

startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);

Should be inside

ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
}

Because startYear, startMonth and startDay are only set on ipDcEventStartDay.setOnClickListener(), so at the beginning they have some value but not the one that was set on ipDcEventStartDay.setOnClickListener()

So in ipDcEventEndDay.setOnClickListener() you have to set again on startDate the values that were picked on the other DatePickerDialog

这篇关于如何在Android日期选择器中将特定选择的日期设置为最小日期?(java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆