使用对话框片段从日期选择器获取日期 [英] Get date from datepicker using dialogfragment
问题描述
我正在使用 google 示例使用对话框片段在我的应用程序中插入日期选择器
http://developer.android.com/guide/topics/ui/controls/pickers.html
I'm using the google example to insert a datepicker inside my app using a dialogfragment
http://developer.android.com/guide/topics/ui/controls/pickers.html
但我不确定如何在设置后获取日期(不是 Java 专家).对话框和日期选择器运行正常,我可以记录该日期已正确设置,但是,我如何才能在父活动上执行回调?
But I'm not sure how to get date after set it (not java expert). Dialog and datepicker runs ok and I can log that date is correctely set but, how can i do to get a callback executed on parent activity?
这是我的对话框片段
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@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);
// 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) {
**Log.w("DatePicker","Date = " + year);**
}
}
...我从我的活动中调用对话...
...and I call dialog from my activity with...
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
在我的父活动而不是 Log.w 中调用方法的正确方法是什么?我想这与将回调作为参数或其他东西传递有关,但我在互联网上找到的大多数参考资料都是关于没有对话框片段的以前的版本
What is the correct way to call a method in my parent activity instead Log.w? I suppose that is something related with passing a callback as parameter or something but most of references I found on internet are about previous versions without dialogfragments
不确定它是否重要,但父活动被声明为:
not sure if it's important but parent activity is declared as:
public class EditSessionActivity extends FragmentActivity {
解决方案:感谢 Lecho 用户,这是解决问题的方法
DatePickerFragmennt.class
DatePickerFragmennt.class
public class DatePickerFragment extends DialogFragment{
@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);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
}
}
...和父活动 EditSessionActivity.class...
...and parent activity EditSessionActivity.class...
public class EditSessionActivity extends FragmentActivity implements OnDateSetListener {
...
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
//do some stuff for example write on log and update TextField on activity
Log.w("DatePicker","Date = " + year);
((EditText) findViewById(R.id.tf_date)).setText("Date = " + year);
}
推荐答案
DatePickerDialog
的构造函数将 DatePickerDialog.OnDateSetListener
作为第二个参数,所以也许你应该在您的父活动 EditSessionActivity
(不在 DatePickerFragment
中)并更改此行:
Constructor fo DatePickerDialog
takes DatePickerDialog.OnDateSetListener
as second parameter, so maybe you should implement that interface in your parent activity EditSessionActivity
(not in DatePickerFragment
) and change this line:
return new DatePickerDialog(getActivity(), this, year, month, day);
进入这个:
return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
然后您的 Activity 应如下所示:
And then your activity should looks like this:
public class EditSessionActivity extends FragmentActivity implements
DatePickerDialog.OnDateSetListener{
public void onDateSet(DatePicker view, int year, int month, int day) {
//use date in your activity
}
...
}
这篇关于使用对话框片段从日期选择器获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!