**你好, 我在android中开发应用程序,我正在使用DatePiker对话框,我试过
@override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
{
if(tvDate.getText().toString().equalsIgnoreCase("--"))
{
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
else
{
String _date[] = tvDate.getText().toString().split("-");
String _monthName = _date[0];
mMonth = helper.getMonthFromName(_monthName) -1;
mDay = Integer.parseInt(_date[1]);
}
return new DatePickerDialog(this,mDateSetListener,mYear, mMonth, mDay);
}
return null;
}
@override
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDate();
if(tvRepeat.getText().toString().equalsIgnoreCase("Weekly")) {
tvDayName.setText(" (" + getDayNameForWeeklyRepeat() + ")");
}
}
};
private void updateDate() {
tvDate.setText(new StringBuilder()
// Month is 0 based so add 1
.append(DateUtils.getMonthString(mMonth,DateUtils.LENGTH_SHORT))
.append("-")
.append(pad(mDay)).append("-")
.append(pad(mYear%100)));
}
所以onCreateDialog
方法只调用一次,但是当我点击下一条记录时,DatePiker对话框显示的是第一次显示的记录的但是我想显示新记录的日期请帮助我< / p>
答案 0 :(得分:2)
来自http://developer.android.com/guide/tutorials/views/hello-datepicker.html
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// capture our View elements
mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
mPickDate = (Button) findViewById(R.id.pickDate);
// add a click listener to the button
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateDisplay();
}
覆盖onCreateDialog
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}
如何更新显示:
// updates the date we display in the TextView
private void updateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}