首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DatePickerDialog setMax()不工作

DatePickerDialog setMax()不工作
EN

Stack Overflow用户
提问于 2017-04-05 09:56:05
回答 3查看 115关注 0票数 0

我有下面的代码,以便setMaxDate() 5天。但是,只有月份列是不合适的。如下图所示,月数超过一个月。我怎么能解决这个问题?

代码语言:javascript
复制
Calendar c = Calendar.getInstance();
        c.setTime(new Date(new Date().getTime() + (1000 * 60 * 24)));
        DatePickerDialog datePickerDialog = new DatePickerDialog(ActivityIncompleteSummary.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        remark.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                    }
                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));

        Calendar today = Calendar.getInstance();
        today.add(Calendar.DAY_OF_MONTH, 5);

        datePickerDialog.getDatePicker().setMinDate(new Date().getTime() + (1000 * 60 * 60 * 24));
        datePickerDialog.getDatePicker().setMaxDate(today.getTimeInMillis());
        datePickerDialog.setOnCancelListener(ActivityIncompleteSummary.this);
        datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (which == DialogInterface.BUTTON_NEGATIVE) {
                    dialog.dismiss();
                }
            }
        });
        datePickerDialog.show();

屏幕截图-添加开头的

截图-在滚动日期间

截图-滚动到最大日

EN

回答 3

Stack Overflow用户

发布于 2017-04-05 10:23:15

将此c.setTime(new Date(new Date().getTime() + (1000 * 60 * 24)));更新为此c.setTime(new Date(new Date().getTime() + (1000 * 60 * 60 * 24)));

还请确保当前日期应大于最小日期和小于最大日期。

票数 0
EN

Stack Overflow用户

发布于 2017-04-05 10:28:08

可以使用下面的方法在日历中添加天数。

代码语言:javascript
复制
   Calendar c = Calendar.getInstance();
   Log.e("Initial",""+c.getTimeInMillis()); // Print current date timestamp
   c.add(Calendar.DATE,4);
   Log.e("after",""+c.getTimeInMillis());// Print Timestamp with 4+ to current date

然后尝试在setMaxDate中选择Picker。

希望它能成功。

票数 0
EN

Stack Overflow用户

发布于 2017-04-05 10:33:37

我也面临着同样的问题。请参考

代码语言:javascript
复制
  static TextView DOB;

 // OnClicking the button we are triggering DatePickerFragment Dialog Calender

 public void OnClickDateBirth(View view) {

        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }



    public 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) + 1;
            int day = c.get(Calendar.DAY_OF_MONTH);

            Log.e("DialogMonth", ""+month);

            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month , day);


           // To Restrict Future Dates

            datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis()); 


            // Create a new instance of DatePickerDialog and return it

              return datePickerDialog;
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something withdate2 the date chosen by the user

               Log.e("Day", ""+day);
               Log.e("Month", ""+month+1);
               Log.e("Year", ""+year);

               boolean booFuture = validateFutureDate(day, month + 1, year);

                Log.e("BooFuture", ""+booFuture);

                if(!booFuture){

                    Toast.makeText(getApplicationContext(), "Future Dates are not selectable", Toast.LENGTH_SHORT).show();


                }else {

                    String day1=""+day;
                    String month1=""+(month+1);

                    if(day1.length()==1)
                        day1 = "0"+day1;
                    if(month1.length()==1)
                        month1 = "0"+month1;

                    DOB.setText(day1 + "-" + month1 + "-" +year );
                    DOB.setError(null);

                }

        }
    }


   // Restrict future date clickable with following method condition

    public static boolean validateFutureDate(int day,int month,int year){
        final Calendar c = Calendar.getInstance();
        int currentYear = c.get(Calendar.YEAR);
        int currentMonth = c.get(Calendar.MONTH)+1;
        int currentDay = c.get(Calendar.DAY_OF_MONTH);

        Log.e("currentDay", ""+currentDay);
        Log.e("Day", ""+day);

        if (day > currentDay && year == currentYear && month == currentMonth) {


            return false;

        } else if (month > currentMonth && year == currentYear) {


            return false;
        } else if (year > currentYear) {


            return false;
        }

        return true;
    } 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43227815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档