gpt4 book ai didi

java - 打开带有建议日期的 DatePicker

转载 作者:行者123 更新时间:2023-12-01 20:57:25 34 4
gpt4 key购买 nike

我需要打开一个 DatePicker,其默认日期基于 GregorianCalendar 的 YEAR-MONTH-DAY_OF_MONTH 属性。

这是我打开 DatePicker 的代码:

DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");

例如,如果我的值(value)观是这样的:

  • MyCalendar.YEAR = 2017
  • MyCalendar.MONTH = 2
  • MyCalendar.DAY_OF_MONTH = 22

打开 DatePicker 时设置的默认值是:

enter image description here

我需要添加什么才能做到这一点?

最佳答案

基本上直接来自Android | Pickers

此外,就像任何其他 fragment 一样,您可以使用 setget-Arguments 将数据传递到 fragment 中。

详细信息:Best practice for instantiating a new Android Fragment

public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

public static DatePickerFragment newInstance(int year,int month,int day) {
Bundle b = new Bundle();
b.putInt("year", year);
// put others...

Fragment f = new DatePickerFragment();
f.setArguments(b);
return f;
}

@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);

// Update using the arguments
Bundle args = getArguments();
if (args != null) {
year = args.getInt("year");
// get others...
}

// 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
}
}

并使用该newInstance方法。

DialogFragment newFragment = DatePickerFragment.newInstance(2017,02,07);
newFragment.show(getFragmentManager(), "datePicker");

关于java - 打开带有建议日期的 DatePicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42100601/

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