0

我遇到了DatePickerDialog的问题,每次打开它时都会显示当前日期而不是之前设置的日期。DatePickerDialog总是打开当前日期

这里是我的代码:

final Calendar c = Calendar.getInstance(); 
    final int mYear = c.get(Calendar.YEAR); 
    final int mMonth = c.get(Calendar.MONTH); 
    final int mDay = c.get(Calendar.DAY_OF_MONTH); 

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

     // when dialog box is closed, below method will be called. 
     public void onDateSet(DatePicker view, int selectedYear, 
       int selectedMonth, int selectedDay) { 
      editText.setText(selectedYear + "年" 
        + selectedMonth + 1 + "月" 
        + selectedDay + "日"); 
     } 
    }; 

    DatePickerDialog datePickerDialog = new DatePickerDialog(SampleActivity.this, 
      datePickerListener, mYear, mMonth, mDay); 

    datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        if (which == DialogInterface.BUTTON_NEGATIVE) { 
         dialog.cancel(); 
        } 
       } 
      }); 

    datePickerDialog.setCancelable(false); 
    datePickerDialog.show(); 

我错过了什么吗?

+1

'Calendar.getInstance() '将使用默认时区返回今天的日期和时间。 –

+0

谢谢,它已被回答。但我的问题是如何给DatePickerDialog'我以前选择的值,下次打开它。 –

回答

2

这可能会帮助您:

public class TestActivity extends Activity { 
private int mYear; 
private int mMonth; 
private int mDay; 

private void showDialog() { 
    if (mYear == 0 || mMonth == 0 || mDay == 0) { 
     final Calendar c = Calendar.getInstance(); 
     mYear = c.get(Calendar.YEAR); 
     mMonth = c.get(Calendar.MONTH); 
     mDay = c.get(Calendar.DAY_OF_MONTH); 
    } 

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

     // when dialog box is closed, below method will be called. 
     public void onDateSet(DatePicker view, int selectedYear, 
       int selectedMonth, int selectedDay) { 
      editText.setText(selectedYear + "年" + selectedMonth + 1 + "月" 
        + selectedDay + "日"); 
      mYear = selectedYear; 
      mMonth = selectedMonth; 
      mDay = selectedDay; 
     } 
    }; 

    DatePickerDialog datePickerDialog = new DatePickerDialog(
      SampleActivity.this, datePickerListener, mYear, mMonth, mDay); 

    datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        if (which == DialogInterface.BUTTON_NEGATIVE) { 
         dialog.cancel(); 
        } 
       } 
      }); 

    datePickerDialog.setCancelable(false); 
    datePickerDialog.show(); 
} 
} 

下面的示例代码将给予完整的解决方案:

public class SecondActivity extends FragmentActivity implements OnClickListener{ 

    private static final String TAG = SecondActivity.class.getName(); 
    private int year; 
    private int month; 
    private int day; 
    private boolean isCancelled; 

    private EditText editText; 

    @Override 
    protected void onCreate(Bundle arg0) { 
     super.onCreate(arg0); 
     setContentView(R.layout.second_activity_layout); 
     editText = (EditText) findViewById(R.id.editText); 
    } 

    public void onButtonClick(View view) { 
     onClick(view); 
    } 

    @Override 
    public void onClick(View v) { 
     if(year == 0) { 
      Calendar calendar = Calendar.getInstance(); 
      day = calendar.get(Calendar.DAY_OF_MONTH); 
      month = calendar.get(Calendar.MONTH); 
      year = calendar.get(Calendar.YEAR); 
     } 
     OnDateSetListener callBack = new OnDateSetListener() { 

      @Override 
      public void onDateSet(DatePicker view, int year, int monthOfYear, 
        int dayOfMonth) { 
       if(isCancelled) { 
        Log.i(TAG, "Cancelled"); 
        isCancelled = false; 
       } else { 
        editText.setText("Date selected"); 
        month = monthOfYear; 
        day = dayOfMonth; 
        SecondActivity.this.year = year; 
       } 
      } 
     }; 
     DatePickerDialog dialog = new DatePickerDialog(this, callBack, year, month, day); 
     dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       if(which == DialogInterface.BUTTON_NEGATIVE) { 
       Log.i(TAG, "Cancel"); 
       isCancelled = true; 
       dialog.dismiss();} 
      } 
     }); 
     dialog.setCancelable(false); 
     dialog.show(); 
    } 
} 
+0

有一个问题,如果我点击取消突出的日期仍然被选中,因为'datePickerListener'我在那里评论'/ /当对话框关闭,下面的方法将被调用.'我如何'setText'如果我点击'只有好吗? –

+0

维护一个布尔变量,当用户点击取消时,使其成为true,并验证onDateSet –

+0

编辑我的代码中的变量值。请参阅示例代码... –

0

取而代之的是,,,

final Calendar c = Calendar.getInstance(); 
    final int mYear = c.get(Calendar.YEAR); 
    final int mMonth = c.get(Calendar.MONTH); 
    final int mDay = c.get(Calendar.DAY_OF_MONTH); 

使用以下

String returnedDate = the date which u already set .. 



    Date dateToEditApp = new Date(); 
       SimpleDateFormat newFormatDate = new SimpleDateFormat(
         "dd MMM yyyy"); 
       try { 
        dateToEditApp = newFormatDate.parse(returnedDate); 
       } catch (ParseException e) { 

        e.printStackTrace(); 
        LoggerFile.logwriter("high",TAG + " ParseException " + e.getMessage()); 
       } 

       Calendar calDate = Calendar.getInstance(); 

       calDate.setTime(dateToEditApp); 

       mYear = calDate.get(Calendar.YEAR); 
       mMonth = calDate.get(Calendar.MONTH); 
       mDay = calDate.get(Cale 

ndar.DAY_OF_MONTH); 
0

只需保存日期,月份和年份值到变量,同时设置日期和下一组使用ONPREPAREDIALOG函数来设置先前设定的日期在你satepicker对话框 - 如 -

private int thisYear, thisMonth, thisday; 

private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() { 
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
      thisYear = year; 
      thisMonth = monthOfYear; 
      thisday = dayOfMonth; 
    } 
}; 

@Override 
protected void onPrepareDialog(int id, Dialog dialog) { 
      ((DatePickerDialog) dialog).updateDate(thisYear , thisMonth , thisday); 
} 
相关问题