如何在“日期选择器"对话框中设置日期限制 [英] How to set the limit on date in Date picker dialog

查看:173
本文介绍了如何在“日期选择器"对话框中设置日期限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置日期限制,以便用户不能选择更多的日期,例如,如果今天是1月1日,则用户应该不能选择超过7个日期,我的意思是他不能选择1月9日.我也希望他不要选择月份和年份.因此,我限制了一周内完成他的任务.

I want to put the limit on date so that user can not pick date more then that, for example if today is 1 January then User should not be able to select more then 7 dates , I mean he can not select 9 January. I also want him not to select the month and year. So I am putting a limit to set his task in one week.

到目前为止,我所做的是显示日期选择器片段并在其中设置当前日期.我主要活动中的代码如下:

what I have done so far is showing the date picker fragment and setting current date in it. the code in my main activity goes like this:

etSelectDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment datePickerFragment = new DatePickerFragment() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        Log.d("Date Change", "onDateSet");
                        Calendar c = Calendar.getInstance();
                        c.set(year, month, day);
                        DateFormat df = DateFormat.getDateInstance();
                        etSelectDate.setText(df.format(c.getTime()));
                        //nextField.requestFocus(); //moves the focus to something else after dialog is closed

                    }
                };
                datePickerFragment.show(MainActivity.this.getSupportFragmentManager(), "datePicker");

            }
        });

和日期选择器片段类如下:

and date picker fragment class goes like this :

 public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
            //blah
        }
    }

直到它可以正常工作,但我不知道如何将日期和月份以及年份的其余部分设置为非选择功能.我已经看到许多链接,例如,但我没有了解我该怎么做,并且在Android网站上也没有任何帮助.

till then its is working fine , but I do not know how to put the limit on date and rest of the months and year should be non Select able . I have seen many link such as like this , but I do not understand How can I do that and also there is nothing helpful on android site.

所以请帮助我,我怎样才能将限制限制为7天

So please help me , How can I put limit of seven days only

更新

通过您的答复,我知道如何在日历中设置最大日期,因此,由于我想将最大日期设置为当前日期的7天,所以我仍然没有得到.到目前为止,我读到的方法是:

Through your replies I know how to set the max date in calender , so As I want to set the max date 7 days ahead of current date , I am still not getting it. the method I read so far is :

pickerDialog.getDatePicker().setMaxDate(new Date().getTime());

它将当前日期设置为最大日期,但是由于它是Date对象,如何在其前添加7天?请帮助

It is setting the current date as maximum, but How can I add 7 days ahead in it since it is Date object ? please help

推荐答案

您可以使用 setMinDate(long) setMaxDate(long)方法.两者都可以在API级别11和更高级别上使用.由于您使用的是 DatePickerDialog ,因此需要首先通过调用 getDatePicker()方法来获取基础的DatePicker.

You have the setMinDate(long) and setMaxDate(long) methods at your disposal. Both of these will work on API level 11 and above. Since you are using a DatePickerDialog, you need to first get the underlying DatePicker by calling the getDatePicker() method.

dpdialog.getDatePicker().setMinDate(minDate);  
dpdialog.getDatePicker().setmaxDate(maxDate);  

来源:在Android中设置DatePickerDialog的限制?

您可以使用来计算minDate,

You can calculate the minDate by using,

Date today = new Date();
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add( Calendar.MONTH, -6 ) // Subtract 6 months
long minDate = c.getTime().getTime() // Twice!  

已更新:

替换下面的行

return new DatePickerDialog(getActivity(), this, year, month, day);

 // Create a new instance of DatePickerDialog and return it
    DatePickerDialog pickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
    pickerDialog.getDatePicker().setMaxDate(maxDate);
    pickerDialog.getDatePicker().setMinDate(minDate);
    return pickerDialog;

这篇关于如何在“日期选择器"对话框中设置日期限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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