2012-09-09 47 views
1

通過在API演示中引用DateWidgets1.java,我傾向於通過以下方式顯示日期選取器小部件。只有在按下完成按鈕時才獲取DatePickerDialog的值

private void initDateTextView() { 
    dateTextView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      showDialog(DATE_DIALOG_ID);     
     }    
    }); 
} 

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DATE_DIALOG_ID: 
     // set date picker as current date 
     return new DatePickerDialog(this, datePickerListener, 
        this.year, this.month, this.date); 
    } 
    return null; 
} 

@Override 
protected void onPrepareDialog(int id, Dialog dialog) { 
    switch (id) { 
    case DATE_DIALOG_ID: 
     ((DatePickerDialog)dialog).updateDate(this.year, this.month, this.date); 
    }   
} 

private static final int DATE_DIALOG_ID = 0; 
private final 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) { 
     Log.i("CHEOK", selectedYear + ", " + selectedMonth + ", " + selectedDay); 
    } 
}; 

但是,我知道,onDateSet將永遠被調用,以最新的微調選擇的日期,無論我按對話框的完成按鈕,或者按電話上的回軟按鈕。

我只對獲取所選日期感興趣,當用戶按完成按鈕,但沒有後退按鈕。

有什麼辦法可以實現嗎?

回答

相關問題