0

我在我的应用程序中使用两个日期选择器来获取“从日期”和“到日期”。当用户第一次单击日历图像按钮(对于两个日期选择器)时,它会显示当前日期,然后单击图像时它再次显示以前选择的日期。

我的问题是我想将两个日期选择器都设置为仅在按下我的应用程序中的重置按钮时才显示当前日期*。*否则它应该显示以前选择的日期。请帮助我实现它。

在此先感谢。以下是代码:`

  DatePickerDialog.OnDateSetListener from_dateListener,to_dateListener;
       .
       .

  from_dateListener = new OnDateSetListener()
  {

      public void onDateSet(DatePicker view, int year, 
              int monthOfYear, int dayOfMonth)
      {
            factiveDate.set(Calendar.YEAR,year) ;
            factiveDate.set(Calendar.MONTH, monthOfYear);
            factiveDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            if(currentDate.before(factiveDate)||(currentDate.equals(factiveDate)))
            {
                if(endDateDisplay.getText().toString().isEmpty())
                {
                    updateStartDisplay();
                    //startPickDate.setClickable(false);
                }
                else
                {
                    if(factiveDate.before(toDate)||(factiveDate.equals(toDate)))
                    {
                        updateStartDisplay();
                        //startPickDate.setClickable(false);
                    }
                    else
                    {
                        Toast t=new Toast(getBaseContext());
                        t=Toast.makeText(Next.this, "To date should be less than or eqaul to from date", Toast.LENGTH_LONG);
                        t.setGravity(Gravity.CENTER, 0, 0);
                        t.show();
                    }
                }
            }
            else
            {
                Toast t=new Toast(getBaseContext());
                t=Toast.makeText(Next.this, "Please enter a valid date", Toast.LENGTH_LONG);
                t.setGravity(Gravity.CENTER, 0, 0);
                t.show();

            }

      }

  };


  to_dateListener = new OnDateSetListener()
     {
          public void onDateSet(DatePicker view, int year, 
                  int monthOfYear, int dayOfMonth)
          {
                tactiveDate.set(Calendar.YEAR, year);
                tactiveDate.set(Calendar.MONTH, monthOfYear);
                tactiveDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                if(currentDate.before(tactiveDate)||(currentDate.equals(tactiveDate)))
                {
                    Log.d("1st if","true");
                    if(!startDateDisplay.getText().toString().isEmpty())
                    {
                        Log.d("2st if","true");
                        if(tactiveDate.after(fromDate)||(tactiveDate.equals(fromDate)))
                        {
                            Log.d("3st if","true");
                            updateEndDisplay();
                            //endPickDate.setClickable(false);
                        }
                        else
                        {
                            Toast t=new Toast(getBaseContext());
                            t=Toast.makeText(Next.this, "To date should be greater than or equal to from date",Toast.LENGTH_LONG );
                            t.setGravity(Gravity.CENTER, 0, 0);
                            t.show();
                        }
                    }
                    else
                        {
                            updateEndDisplay();
                            //endPickDate.setClickable(false);
                        }
                    }
                else
                {
                    Toast t=new Toast(getBaseContext());
                    t=Toast.makeText(Next.this, "Please enter a valid date", Toast.LENGTH_LONG);
                    t.setGravity(Gravity.CENTER, 0, 0);
                    t.show();
                }


          }
      };


}
private void updateStartDisplay()
{
    startDateDisplay.setText(
            new StringBuilder()
                // Month is 0 based so add 1

                .append(factiveDate.get(Calendar.DAY_OF_MONTH)).append("-")
                .append(factiveDate.get(Calendar.MONTH)+1).append("-")
                .append(factiveDate.get(Calendar.YEAR)).append(" "));

     fromDate=factiveDate; 
     factiveDate=currentDate;
     factiveDate=null;
        //Log.d("msg","date:"+(date.get(Calendar.DAY_OF_MONTH))+(date.get(Calendar.MONTH)+1)+(date.get(Calendar.YEAR)));



    }

  private void updateEndDisplay()
  {
        endDateDisplay.setText(
                new StringBuilder()
                    // Month is 0 based so add 1

                    .append(tactiveDate.get(Calendar.DAY_OF_MONTH)).append("-")
                    .append(tactiveDate.get(Calendar.MONTH) + 1).append("-")
                   .append(tactiveDate.get(Calendar.YEAR)).append(" "));

        toDate=tactiveDate;
        tactiveDate=null;
        //Log.d("msg","date:"+(date.get(Calendar.DAY_OF_MONTH))+(date.get(Calendar.MONTH)+1)+(date.get(Calendar.YEAR)));

  }   









   /* capture our View elements for the end date function */


    @Override
    protected Dialog onCreateDialog(int id) {

        switch(id){
            case DATE_PICKER_FROM:
                    Log.d("current date-on create"," "+(currentDate.get(Calendar.YEAR))+ (currentDate.get(Calendar.MONTH)+1)+ (currentDate.get(Calendar.DAY_OF_MONTH)));
                    return new DatePickerDialog(this, from_dateListener, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH));


                case DATE_PICKER_TO:
                    return new DatePickerDialog(this, to_dateListener, currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH));
        }
            return null;
    }

}

4

1 回答 1

0

您可以使用 DatePicker 设置默认值

dPicker.init((previouslyEnteredDate.getYear() + 1900),
                        previouslyEnteredDate.getMonth(),
                        previouslyEnteredDate.getDate(), null);

希望能帮助到你。

于 2013-04-12T08:55:56.860 回答