我有以下方法,我从jodatime库中获取日期,我使用了datepicker和timpicker让用户选择日期和时间。我已经创建了一个按钮,点击该按钮,日期选择器获取所选日期并显示在按钮上,但问题是,如果我重新打开日期选择器,则第二次显示当前日期和时间,而不是之前选择的日期。
这是我的代码,
@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函数。任何人都可以告诉我如何使日期选择器显示所选日期,即使是第二次吗?
这是代码的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" />
有人可以建议一个解决方案吗?
答案 0 :(得分:0)
如果您想在下次调用时将时间设置为日期,则必须按如下方式对其进行初始化
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
datepicker.init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
请注意,Month将以0到11的整数设置,其中0为jan。 如果您不想使用onDateChangeListener,则可以将其设置为null。
在findViewByID下面初始化你的日期选择器,不要让它成为最终的。