【问题标题】:How to prevent users from setting the date and time that is before the current date and time如何防止用户设置早于当前日期和时间的日期和时间
【发布时间】:2012-09-05 03:27:58
【问题描述】:

我的应用程序遇到问题。我正在尝试做一些事情来阻止用户选择已经过去的日期和时间。如果用户选择过去的日期和时间,将显示错误消息。用户将从 datePicker 和 timePicker 中选择日期和时间。有人对这样做有任何想法吗?任何帮助将不胜感激。非常感谢!

【问题讨论】:

    标签: android date time datepicker timepicker


    【解决方案1】:

    您可以通过广播接收器 ACTION_TIME_CHANGED 阻止用户更改日期时间。如果用户更改日期时间,则广播接收器将调用。覆盖广播接收器中的 onRecieve 方法。请确保在清单文件中定义广播接收器

      @Override
      public void onReceive(Context context, Intent intent) {
    
        // You can put conditions the changed date time is not past from the current time and show toast message.
    
      }
    

    【讨论】:

      【解决方案2】:

      如果您在 edittext 中设置选择器值,您可以这样做:

      Calendar cal = Calendar.getInstance();
                                      cal.set(cal.get(Calendar.YEAR),
                                              cal.get(Calendar.MONTH),
                                              cal.get(Calendar.DAY_OF_MONTH));
                                      DateFormat df = new SimpleDateFormat(
                                              "dd/MM/yyyy");
                                      Date day_entered, day_valid;
      
                                      day_entered = df.parse(your_date_edittext.getText()
                                              .toString());
                                      day_valid = df.parse(df.format(cal.getTime()));
                                      if (day_entered.after(day_valid)) {
                                          Toast msg = Toast
                                                  .makeText(
                                                          Profile.this,
                                                          "Please Enter a valid date",
                                                          Toast.LENGTH_LONG);
                                          msg.show();
                                          }
      

      【讨论】:

      • 嗨,你在edittext中设置选择器值是什么意思?我直接从选择器中设置值。
      • 您是否正在设置从选择器中获取的值,例如edittext、textview 之类的?
      • 哦,是的,我将选择器中的值传递给 textview。
      • 然后在我的代码中,您在我的代码中传递该值而不是 your_date_edittext.getText().toString());,或者您可以简单地使用 setmindate() 函数:P
      • 请看this
      【解决方案3】:
      if(fromdate!=null && fromdate.length()>0 && todate!=null && todate.length()>0)
         {
          try
             {
            sdf = new SimpleDateFormat("yyyy-mm-dd");
                     date1 = sdf.parse(fromdate);
                     date2 = sdf.parse(todate);
      
                 Calendar cal = Calendar.getInstance();
                 Calendar cal2 = Calendar.getInstance();
      
      
                 cal.setTime(date1);
                       cal2.setTime(date2);
      
      
                        if(cal.after(cal2)){
                      System.out.println("Date1 is after Date2");
                    //  Toast.makeText(getApplicationContext(), "from Date1 is after Date2", Toast.LENGTH_SHORT).show();
                     // clear the dates here........
                        }
      
      
             }
            catch(Exception e)
            {
             Log.d("pavan","in exception block ");
            }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多