在DatePickerDialog在Android中设置的限制? [英] Set Limit on the DatePickerDialog in Android?

查看:192
本文介绍了在DatePickerDialog在Android中设置的限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何限制对DatePickerDialog选择日期从2-12岁,在过去0-2年从目前的日期。我有DatePickerDialog的活动是用于获取的出生日期为儿童和婴儿.Child年龄会(2-12yrs)和婴儿年龄可以是(0-2ys)。那么,什么可以做些什么来让我的calender.If这个功能我通过1,那么年龄限制会(2-12yrs)和我通过2年龄限制会(0-2yrs)。请帮助得到的功能在我的日历。

我的日历code 是:

 日期=新DatePickerDialog.OnDateSetListener(){

            @覆盖
            公共无效onDateSet(DatePicker的观点,年整型,诠释monthOfYear,
                    INT DAYOFMONTH){
                // TODO自动生成方法存根
                myCalendar.set(Calendar.YEAR,一年);
                myCalendar.set(Calendar.MONTH,monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH,DAYOFMONTH);
                字符串myFormat =DD / MM / YY; //中,你需要把这里
                SimpleDateFormat的SDF =新的SimpleDateFormat(myFormat,Locale.US);
                dateofBirth.setText(sdf.format(myCalendar.getTime()));
            }
        };

dateofBirth.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                的ShowDialog(DATE_DIALOG_ID);

            }
        });

公共对话onCreateDialog(INT ID){
    最后的日历现在= Calendar.getInstance();

    开关(ID){
    案例DATE_DIALOG_ID:
        //设置日期选取器为当前日期
        DatePickerDialog _date =新DatePickerDialog(本,日期,myCalendar
                获得(Calendar.YEAR),myCalendar.get(Calendar.MONTH)
                myCalendar.get(Calendar.DAY_OF_MONTH)){
            @覆盖

            公共无效onDateChanged(DatePicker的观点,年整型,诠释monthOfYear,诠释DAYOFMONTH){

                如果(年> now.get(Calendar.YEAR))

                    view.updateDate(myCalendar
                            获得(Calendar.YEAR),myCalendar
                            获得(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH));

                如果(monthOfYear> now.get(Calendar.MONTH)及和放大器;一年== now.get(Calendar.YEAR))
                    view.updateDate(myCalendar
                            获得(Calendar.YEAR),myCalendar
                            获得(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH));

                如果(DAYOFMONTH> now.get(Calendar.DAY_OF_MONTH)及和放大器;一年== now.get(Calendar.YEAR)及和放大器;
                        monthOfYear == now.get(Calendar.MONTH))
                    view.updateDate(myCalendar
                            获得(Calendar.YEAR),myCalendar
                            获得(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH));
            }

        };

        返回_date;
    }
    返回null;
}
 

解决方案

所有其他的答案似乎颇为曲折,所以我只是陈述显而易见的:你可以得到基本的DatePicker DatePickerDialog (由C $ C>简单地调用<一href="http://developer.android.com/reference/android/app/DatePickerDialog.html#getDatePicker%28%29"><$c$c>getDatePicker())并利用设置其边界:

  • <一个href="http://developer.android.com/reference/android/widget/DatePicker.html#setMinDate%28long%29"><$c$c>setMinDate(long为MinDate)
  • <一个href="http://developer.android.com/reference/android/widget/DatePicker.html#setMaxDate%28long%29"><$c$c>setMaxDate(long的maxDate)

当参数是毫秒的平常人数自1月1日,在默认时区1970 00:00:00。你还是要计算当然这些数值,但应该是容易做到的日历类:只取当前日期和添加或substract X 年。

更细心的读者会发现,前面提到的制定者并没有用,直到API级别11.如果您定位的(或更新版本)的平台而已,那么你是好去。如果你也想支持即姜饼设备(Android 2.3的/ API级别9 +),您可以使用回迁版的DatePicker 在代替。

How to limit on DatePickerDialog to choose date from 2-12 years in the past and from 0-2 year from the current date . I have DatePickerDialog in activity which is used for getting the date of birth for child and infant .Child age will be (2-12yrs)and infant age can be (0-2ys). SO what could i do to get this functionality in my calender.If i pass 1 then the age limit will be (2-12yrs) and i pass 2 age limit will be (0-2yrs).Please help to get the functionality in my calender.

My Calender Code is :

date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                String myFormat = "dd/MM/yy"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                dateofBirth.setText(sdf.format(myCalendar.getTime()));
            }
        };

dateofBirth.setOnClickListener(new OnClickListener() {

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

            }
        });

public Dialog onCreateDialog(int id) {
    final Calendar now = Calendar.getInstance();

    switch (id) {
    case DATE_DIALOG_ID:
        // set date picker as current date
        DatePickerDialog _date =   new DatePickerDialog(this, date,myCalendar
                .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)){
            @Override

            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth){   

                if (year > now.get(Calendar.YEAR))

                    view.updateDate(myCalendar
                            .get(Calendar.YEAR), myCalendar
                            .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));

                if (monthOfYear > now.get(Calendar.MONTH) && year == now.get(Calendar.YEAR))
                    view.updateDate(myCalendar
                            .get(Calendar.YEAR), myCalendar
                            .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));

                if (dayOfMonth > now.get(Calendar.DAY_OF_MONTH) && year == now.get(Calendar.YEAR) && 
                        monthOfYear == now.get(Calendar.MONTH))
                    view.updateDate(myCalendar
                            .get(Calendar.YEAR), myCalendar
                            .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
            }

        };

        return _date;
    }
    return null;
}

解决方案

All the other answers seem rather convoluted, so I'm just going to state the obvious: you can get the underlying DatePicker from a DatePickerDialog (by simply calling getDatePicker()) and set its bounds using:

Where the argument is the usual number of milliseconds since January 1, 1970 00:00:00 in the default time zone. You'll still have to calculate these values of course, but that should be trivial to do with the Calendar class: just take the current date and add or substract x years.

The more attentive reader will notice that the aforementioned setters weren't available until API level 11. If you're targeting that (or newer) platform only, then you're good to go. If you also want to support i.e. Gingerbread devices (Android 2.3 / API level 9+), you can use a backported version of DatePicker in stead.

这篇关于在DatePickerDialog在Android中设置的限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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