我有以下方法我在哪裏取出由jodatime庫日期和我使用了一個日期選擇器和timpicker讓用戶選擇的日期和時間。我已經做了一個按鈕,按鈕上的日期選取器提取所選日期並顯示在按鈕上,但問題是,如果我第二次重新打開日期選擇器,它會顯示當前日期和時間,而不是先前選擇的日期。jodatime上獲取日期和更新日期選擇器對話框datechanged
這裏是我的代碼,
@OnClick(R.id.Button_due_date)
public void show_DateTimePicker() {
final View dialogView = View.inflate(AddFollowUp.this, R.layout.datetime_picker, null);
final AlertDialog alertDialog = new AlertDialog.Builder(AddFollowUp.this).create();
dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);
DateTime dateTime_jodatime = new DateTime(
datePicker.getYear(),
datePicker.getMonth() + 1,
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute(),
0
);
mFollowUpTime =datePicker.getYear()+ "-";
if ((datePicker.getMonth() + 1) < 10) {
mFollowUpTime =mFollowUpTime+ "0" + (datePicker.getMonth() + 1) + "-";
} else
mFollowUpTime =mFollowUpTime+ (datePicker.getMonth() + 1) + "-";
if (datePicker.getDayOfMonth() < 10)
mFollowUpTime = mFollowUpTime + "0" + datePicker.getDayOfMonth()+ " ";
else
mFollowUpTime = mFollowUpTime + datePicker.getDayOfMonth() + " ";
if (timePicker.getCurrentHour() < 10)
mFollowUpTime = mFollowUpTime + "0" + timePicker.getCurrentHour() + ":";
else
mFollowUpTime = mFollowUpTime + timePicker.getCurrentHour() + ":";
if (timePicker.getCurrentMinute() < 10)
mFollowUpTime = mFollowUpTime + "0" + timePicker.getCurrentMinute();
else
mFollowUpTime = mFollowUpTime + timePicker.getCurrentMinute();
String month_text = dateTime_jodatime.monthOfYear().getAsText();
StringBuffer date = new StringBuffer();
date.append(dateTime_jodatime.getDayOfMonth() + " ");
date.append(month_text.substring(0, 3) + " ");
date.append(dateTime_jodatime.getYear() + " ");
date.append(dateTime_jodatime.getHourOfDay() + ":");
date.append(dateTime_jodatime.getMinuteOfHour() + "");
datePicker.updateDate(dateTime_jodatime.getYear(),dateTime_jodatime.getMonthOfYear(),dateTime_jodatime.getDayOfMonth());
String day = CommonUtils.AddZero(dateTime_jodatime.monthOfYear().get());
final String month = CommonUtils.AddZero(dateTime_jodatime.getDayOfMonth());
String year = CommonUtils.AddZero(dateTime_jodatime.getYear());
String hour = CommonUtils.AddZero(dateTime_jodatime.getHourOfDay());
String minute = CommonUtils.AddZero(dateTime_jodatime.getMinuteOfHour());
DonorDetails.Actual_Due_Date = day + "-" + month + "-" + year + " " + hour + ":" + minute + ":" + "00";
mButton_due_date.setText(date);
alertDialog.dismiss();
}
});
alertDialog.setView(dialogView);
alertDialog.show();
}
我已經試過datepicker.ondatechanged監聽器,但它似乎沒有工作,我也嘗試過用datepicker.updatedate函數,它似乎並沒有工作。任何人都可以告訴我如何使datepicker顯示選定的日期,即使第二次?
這裏是我的代碼的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:calendarViewShown="false"
android:spinnersShown="true" />
<TimePicker
android:id="@+id/time_picker"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4" />
<Button
android:id="@+id/date_time_set"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Set" />
任何人都可以請提出一個解決辦法?
你可以通過選擇日期和時間,在該方法中你要調用的顯示日期和時間選擇器和參數只是用你可以設置所選擇的日期 –
u能告訴我如何價值? –
請參閱本http://www.tutorialspoint.com/android/android_datepicker_control.htm –