我有DatePicker。如果我使用键盘更改值(年),则此值保持不变。只有当我点击按钮" +"或" - "价值变化.... 它是我的倾听者:
datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mDate.set(Calendar.YEAR, year);
mDate.set(Calendar.MONTH, monthOfYear);
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
getArguments().putSerializable(EXTRA_DATE, mDate);
}
});
和TimePicker的类似问题(改变任何东西)
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
mTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
mTime.set(Calendar.MINUTE, minute);
getArguments().putSerializable(EXTRA_TIME, mTime);
}
})
和内部片段
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode != Activity.RESULT_OK) return;
if(requestCode == REQUEST_DATE){
Calendar date = (Calendar)data.getSerializableExtra(DateFragment.EXTRA_DATE);
mObject.setDate(date);
SimpleDateFormat dateFormat = new SimpleDateFormat("EE, dd.MM.yy", Locale.US);
mDateButton.setText(dateFormat.format(mObject.getDate().getTime()));
}
if(requestCode == REQUEST_TIME){
Calendar date = (Calendar)data.getSerializableExtra(TimeFragment.EXTRA_TIME);
mObject.setDate(date);
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm, a", Locale.US);
mTimeButton.setText(dateFormat.format(mObject.getDate().getTime()));
}
}
答案 0 :(得分:1)
DatePicker
只有在失去焦点EditText
之后才会回复日期更改。
因此,您可以使用datePicker.clearChildFocus(getCurrentFocus())
清除它的焦点,然后获取它的日期值。