【问题标题】:How to restrict user from choosing past dates from date picker?如何限制用户从日期选择器中选择过去的日期?
【发布时间】:2014-08-18 10:15:55
【问题描述】:

我正在使用日期选择器来设置警报,但这也允许用户在当前日期之前设置日期这是我设置日期选择器的代码

public void onClick(View v) {

            new DatePickerDialog(ReminderDetailActivity.this, date,
                    myCalendar.get(Calendar.YEAR), myCalendar
                            .get(Calendar.MONTH), myCalendar
                            .get(Calendar.DAY_OF_MONTH)).show();

        }

谁能告诉我限制用户去过去的日期

提前谢谢你

【问题讨论】:

标签: android datepicker


【解决方案1】:

你可以使用这样的东西。

Calendar c = Calendar.getInstance();
int todaysDate = c.get(Calendar.DATE);

 public void onClick(View v) {

            new DatePickerDialog(ReminderDetailActivity.this, date,
                    myCalendar.get(Calendar.YEAR), myCalendar
                            .get(Calendar.MONTH), myCalendar
                            .get(Calendar.DAY_OF_MONTH)).show();

                         if(todaysDate < date){
                    Toast.makeText(Activity.this, "choose a future date",Toast.LENGTH_SHORT).show();
    }else{
                    //execute normal function
    }


   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 2012-09-09
    • 2020-03-17
    • 1970-01-01
    • 2017-03-02
    相关资源
    最近更新 更多