在過去的24小時內,我一直在試圖找到關於我的問題的文章,但我仍然感到困惑。如果我錯過了曾經回答過這個問題的地方,我很抱歉,但是我希望你們都會幫助我理解我錯過了什麼。試圖從android日期選擇器返回日期
劇情簡介:我想用日期選取器填充幾個文本框,並且無法理解如何返回選定的日期,以便我可以填充相應的文本框。
基本上我試圖實現一個標準的日期選擇器針對android設備> = 4.0。
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
你會發現,這是從Android的文檔和示例代碼,可以在這裏找到:http://developer.android.com/guide/topics/ui/controls/pickers.html 我實現的一個大項目的代碼和日期選擇器自帶了罰款,日期被挑細等等。 我遇到麻煩的是在用戶選擇它之後得到日期。基本上用戶選擇日期然後什麼都不會發生。我想要做的是在選擇後填入具有日期的特定文本框。這將發生在幾個文本框中,所以我不能將它硬編碼到onDateSet函數中。 我的想法是將文本框的id與創建的datepicker一起傳遞,但我不完全確定我會如何做到這一點。
這是我第一個android項目,專注於理解,而不是簡單地得到一個工作項目,謝謝。
onDateSet在用戶選擇日期時觸發。我不知道如何有足夠的上下文知道哪個對話框更新時,該功能被觸發。問題是我的活動有多個地方輸入日期,所以每次觸發我需要更新一個不同的文本框。我不知道如何在日期最終被選中時通過文本框更新到函數。 –
噢好吧,所以我覺得不要實現OnDateSetListener,而應該在創建DatePickerDialog時創建一個新的OnDateSetListener。這樣,你將有多個「onDateSet」方法。 –