日期选择器不保存所选日期

时间:2019-05-29 17:42:39

标签: android datepicker calendar

选择日期,月份和年份时,请始终保存今天的时间。这就是我尝试过的

private DatePicker.OnDateChangedListener dateSetListener = new DatePicker.OnDateChangedListener() {
       public void onDateChanged(DatePicker view, int año, int mes,
                                 int dia) {
           isDateChanged = true;
       }
   };
private void AgregarCumpleaños() {
       if (isDateChanged = false)
       {
           Toast.makeText(this, "Elija su cumpleaños", Toast.LENGTH_SHORT).show();
       }
       else
       {
           datePicker = findViewById(R.id.cumpleano);
           final Calendar ca = Calendar.getInstance();
           int dia =  ca.get(Calendar.DAY_OF_MONTH);
           int mes = ca.get(Calendar.MONTH);
           int año = ca.get(Calendar.YEAR);

           final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM");
           saveCurrentDate = dateFormatter.format(ca.getTime());

我需要保存所选的月份和日期或将其保存在其变量中

1 个答案:

答案 0 :(得分:0)

我看不到OnDateChangedListener的任何工具

DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

                @Override
                public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
                    Log.d("Date", "Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth);

                }
            });