gpt4 book ai didi

android - 将选定日期设置为日期选择器对话框

转载 作者:行者123 更新时间:2023-12-05 00:11:24 26 4
gpt4 key购买 nike

在用户选择日期并重新打开日期选择器对话框后,它设置为当前日期...不是所选日期...在用户重新打开日期选择器对话框后如何将所选日期设置为日期选择器,这是我的代码

@Override
public void onClick(View v) {
Dialog.Builder builder = null;
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR)+10;
c.set(year, c.get(Calendar.MONTH), c.get(Calendar.DATE));
builder = new DatePickerDialog.Builder(R.style.CustomStyle_DatePicker) {
@Override
public void onPositiveActionClicked(DialogFragment fragment) {
DatePickerDialog dialog = (DatePickerDialog) fragment.getDialog();
String date = dialog.getFormattedDate(SimpleDateFormat.getDateInstance());
selected_date.setText(date);
super.onPositiveActionClicked(fragment);
}

@Override
public void onNegativeActionClicked(DialogFragment fragment) {
super.onNegativeActionClicked(fragment);
}
}.dateRange(System.currentTimeMillis() - 1000, c.getTimeInMillis());
builder.positiveAction("OK")
.negativeAction("CANCEL");
DialogFragment fragment = DialogFragment.newInstance(builder);;
fragment.show(getFragmentManager(), null);
}

我四处搜索,但我无法让它工作,有什么帮助吗?

最佳答案

全局声明:

Calendar selectedDate = Calendar.getInstance();

然后将您的日期设置为构建器,并在用户选择日期时将其保存为 selectedDate,如下所示。这将设置当前日期,直到用户选择一个日期。

@Override
public void onClick(View v) {
Dialog.Builder builder = null;
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR)+10;
c.set(year, c.get(Calendar.MONTH), c.get(Calendar.DATE));
builder = new DatePickerDialog.Builder(R.style.CustomStyle_DatePicker) {
@Override
public void onPositiveActionClicked(DialogFragment fragment) {
DatePickerDialog dialog = (DatePickerDialog) fragment.getDialog();
String date = dialog.getFormattedDate(SimpleDateFormat.getDateInstance());
selected_date.setText(date);
selectedDate.set(dialog.getYear(),dialog.getMonth(), dialog.getDay());
super.onPositiveActionClicked(fragment);
}

@Override
public void onNegativeActionClicked(DialogFragment fragment) {
super.onNegativeActionClicked(fragment);
}
}.dateRange(System.currentTimeMillis() - 1000, c.getTimeInMillis()).date(selectedDate.get(Calendar.DATE),
selectedDate.get(Calendar.MONTH)
, selectedDate.get(Calendar.YEAR));
builder.positiveAction("OK")
.negativeAction("CANCEL");
DialogFragment fragment = DialogFragment.newInstance(builder);
fragment.show(getFragmentManager(), null);
}

关于android - 将选定日期设置为日期选择器对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36145495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com