2015-11-06 64 views
0

enter image description hereDatePicker和TimePicker更改值

我有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())); 
    } 
} 

回答

1

DatePicker只有失去重心從它的年EditText後日期變化做出反應。

所以你可以清除它的焦點datePicker.clearChildFocus(getCurrentFocus()),然後得到它的日期值。

+0

在哪裏插入?我試過但getCurrentFocus無法解析方法...... –

+0

你在對話框中使用它嗎? –

+0

是的,我用在dialogFragment裏面... –