我有一个DatePickerDialog.在默认配置中,它工作正常.
DatePickerDialog dialog = new DatePickerDialog(
view.getContext(),
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker picker, int year, int monthOfYear, int dayOfMonth) {
// do something
}
},
startDate.getYear(),
startDate.getMonth() - 1,
startDate.getDay()
);
现在我想更改按钮的文本.我尝试使用以下代码:
dialog.setButton(DatePickerDialog.BUTTON_POSITIVE, "New Label", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do something
}
});
现在我的问题是从datepicker控件获取日期.单击按钮不会调用DateSetListener.如何通过DateSetListener获取日期?
获得约会的另一种可能性是
public void onClick(DialogInterface dialog, int which) {
int year = ((DatePickerDialog) dialog).getDatePicker().getYear();
int month = ((DatePickerDialog) dialog).getDatePicker().getMonth();
int day = ((DatePickerDialog) dialog).getDatePicker().getDayOfMonth();
}
但这似乎是一种不必要的复杂方式.
对于TimePickerDialog,这种替代方法不起作用,因为我找不到获取timepicker控件的方法.因此我需要使用TimeSetListener,但我找不到可能性(比如datepicker).
谢谢你的回复.
解决方法:
我认为这将适用于DatePicker,它适用于TimePicker:
听众:
TimePickerDialog.OnTimeSetListener mTimeSetListenerIni =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(android.widget.TimePicker view,
int hourOfDay, int minute) {
Log.i("","Initial: "+hourOfDay+":"+minute);
}
};
TimePickerDialog yourFragment = new TimePickerDialog(this, mTimeSetListenerIni, hour, minute, true);
yourFragment.setButton(DialogInterface.BUTTON_POSITIVE, "OK", yourFragment);
yourFragment.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", yourFragment);