datepicker方法重复两次

时间:2015-01-21 09:37:18

标签: android

如果我点击它,我有一个图像,日期选择器正在显示。如果我选择日期和完成,则会显示日期。

它的工作正常但是System.out.println("Current Date2 is greater");                  控制台中Toast.makeText(getApplicationContext(), "Invalid Date", Toast.LENGTH_SHORT).show();获取数据2次。它重复2次。

    public class LoginActivity extends ActionBarActivity implements OnClickListener  {

     private Calendar calendar;
     private int day;
     private int month;
     private int year;

    private ImageView dateOfBirthImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

            dateOfBirthImage = (ImageView) findViewById(R.id.dobImage);
            calendar = Calendar.getInstance();
            day = calendar.get(Calendar.DAY_OF_MONTH);
            month = calendar.get(Calendar.MONTH) + 1;
            year = calendar.get(Calendar.YEAR);

            dateOfBirthImage.setOnClickListener(this);

    }


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



     @Override
       protected Dialog onCreateDialog(int id) {
       // TODO Auto-generated method stub

           //  return new DatePickerDialog(this, datePickerListener, year, month, day);
             if (id == 999) {
                 return new DatePickerDialog(this, datePickerListener, year, month, day);
               }
              return null;
       }


     private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

      public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {

          SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
          try {

              String currentDate = ""+day + "/" + month  + "/" + year ;           
              String selectedDate = ""+selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear ;

             java.util.Date date1 = sdf.parse(currentDate);
             java.util.Date date2 = sdf.parse(selectedDate);          

             System.out.println("Current compare " + currentDate + "sasd" + selectedDate );

             if (date1.compareTo(date2) < 0 ) {

                 System.out.println("Current Date2 is greater");
                 Toast.makeText(getApplicationContext(), "Invalid Date", Toast.LENGTH_SHORT).show();

                }
                  else {

                }


        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


      }
     };
}

1 个答案:

答案 0 :(得分:0)

使用DatePicker方法

检查isShown视图
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
if(view.isShown()){
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      try {

          String currentDate = ""+day + "/" + month  + "/" + year ;           
          String selectedDate = ""+selectedDay + "/" + (selectedMonth + 1) + "/" + selectedYear ;

         java.util.Date date1 = sdf.parse(currentDate);
         java.util.Date date2 = sdf.parse(selectedDate);          

         System.out.println("Current compare " + currentDate + "sasd" + selectedDate );

         if (date1.compareTo(date2) < 0 ) {

             System.out.println("Current Date2 is greater");
             Toast.makeText(getApplicationContext(), "Invalid Date", Toast.LENGTH_SHORT).show();

            }
              else {

            }


    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

   }
  }