问题描述
因此,我创建了一个活动日历对象c和设置时间为2016年2月29日,我想要的片段对话加载日期作为默认日期。我缺少的是在这里吗?我是否需要删除片段日历对象CAL?如果是这样,在片段onCreateDialog怎么办我引用或获得()从set()方法在活动日期?
从活动文件:
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
...
最后的日历C = Calendar.getInstance();
c.set(Calendar.YEAR,2016年);
c.set(的Calendar.MONTH,1);
c.set(Calendar.DAY_OF_MONTH,29); DialogFragment newFragment =新DatePickerFragment();
newFragment.show(getSupportFragmentManager(),日期选择器);
从片段文件:
...
@覆盖
公共对话框onCreateDialog(捆绑savedInstanceState){ 日历CAL = Calendar.getInstance(); currentyear = cal.get(Calendar.YEAR);
currentmonth = cal.get(的Calendar.MONTH);
CURRENTDAY = cal.get(Calendar.DAY_OF_MONTH); DatePickerDialog dateDialog =新DatePickerDialog(this.getActivity(),这一点,currentyear,currentmonth,CURRENTDAY);
返回dateDialog;
的日历ç
您在活动设置是从日历不同的CAL
在你的片段,它们是两个不同的实例。
我建议你使用过的年,月,日的片段捆绑
,所以你会想要去的:
捆绑数据=新包();
data.putInt(...,...); //
DialogFragment DF =新DialogFragment();
df.setArguments(数据);
然后在你的 onCreateDialog
中的片段,利用检索数据:
年整型= getArguments(...);
So I created a calendar object "c" in the Activity and set the date to February 29, 2016. I want the fragment dialog to load that date as the default date. What am I missing here? Do I need to delete the fragment calendar object 'cal'? If so, in the fragment onCreateDialog how do I reference or get() the date from the set() method in the Activity?
from Activity file:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
final Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2016);
c.set(Calendar.MONTH, 1);
c.set(Calendar.DAY_OF_MONTH, 29);
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
from the Fragment file:
...
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar cal = Calendar.getInstance();
currentyear = cal.get(Calendar.YEAR);
currentmonth = cal.get(Calendar.MONTH);
currentday = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dateDialog = new DatePickerDialog(this.getActivity(), this, currentyear,currentmonth,currentday);
return dateDialog;
The Calendar c
you set in your Activity is different from the Calendar cal
in your fragment, they are two different instances.
I suggest you pass the year, month and day to the fragment using a Bundle
, so you would want to go:
Bundle data = new Bundle();data.putInt("...", "..."); //DialogFragment df = new DialogFragment();df.setArguments(data);
Then in your onCreateDialog
in the fragment, retrieve the data using:
int year = getArguments("...");
这篇关于如何设置在DatePickerDialog在我的片段从主机活动的默认日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!