获取日期使用dialogfragment日期选择器 [英] Get date from datepicker using dialogfragment
问题描述
我使用的是谷歌的例子使用dialogfragment
插在我的应用程序中的日期选择器
<一href="http://developer.android.com/guide/topics/ui/controls/pickers.html">http://developer.android.com/guide/topics/ui/controls/pickers.html
但我不知道怎么去后设置日期(不是Java专家)。对话和日期选择器运行正常,我可以登录的日期是correctely设置,但是,我该怎么做才能得到父母的活动执行的回调?
这是我的对话片段
公共类DatePickerFragment扩展DialogFragment工具
DatePickerDialog.OnDateSetListener {
@覆盖
公共对话onCreateDialog(包savedInstanceState){
//使用当前日期作为选择器的默认日期
最后的日历C = Calendar.getInstance();
INT年= c.get(Calendar.YEAR);
INT月= c.get(Calendar.MONTH);
INT天= c.get(Calendar.DAY_OF_MONTH);
//创建DatePickerDialog的新实例,并将其返回
返回新DatePickerDialog(getActivity(),这,年,月,日);
}
公共无效onDateSet(DatePicker的观点,年整型,INT月,日整型){
** Log.w(DatePicker的,日期=+一年)**
}
}
......我呼吁对话从我的活动与...
公共无效showDatePickerDialog(视图v){
DialogFragment newFragment =新DatePickerFragment();
newFragment.show(getSupportFragmentManager(),日期选择器);
}
什么是叫我的父母,而不是活动的一个Log.w方法,正确的方法是什么?我想这是一些与传递一个回调的参数或东西,但大多数引用我发现在互联网上的相关约为previous版本,而dialogfragments
编辑:不知道,如果是重要的,但父活动被声明为:
公共类EditSessionActivity扩展FragmentActivity {
解决方案:由于Lecho用户,这是要做到这一点
DatePickerFragmennt.class
公共类DatePickerFragment扩展DialogFragment {
@覆盖
公共对话onCreateDialog(包savedInstanceState){
//使用当前日期作为选择器的默认日期
最后的日历C = Calendar.getInstance();
INT年= c.get(Calendar.YEAR);
INT月= c.get(Calendar.MONTH);
INT天= c.get(Calendar.DAY_OF_MONTH);
//创建DatePickerDialog的新实例,并将其返回
返回新DatePickerDialog(getActivity(),(EditSessionActivity)getActivity(),年,月,日);
}
}
...和父活动EditSessionActivity.class ...
公共类EditSessionActivity扩展FragmentActivity实现OnDateSetListener {
...
@覆盖
公共无效onDateSet(DatePicker的观点,年整型,INT月,日整型){
//做一些东西,例如写上活动日志和更新文本字段
Log.w(DatePicker的,日期=+一年);
((的EditText)findViewById(R.id.tf_date))的setText(日期=+一年);
}
构造FO DatePickerDialog
需要 DatePickerDialog.OnDateSetListener
作为第二个参数,那么也许你应该实现该接口在父活动 EditSessionActivity
(而不是在 DatePickerFragment
),并改变这种行:
返回新DatePickerDialog(getActivity(),这,年,月,日);
这个:
返回新DatePickerDialog(getActivity(),(EditSessionActivity)getActivity(),年,月,日);
和那么你应该活动看起来是这样的:
公共类EditSessionActivity扩展FragmentActivity工具
DatePickerDialog.OnDateSetListener {
公共无效onDateSet(DatePicker的观点,年整型,INT月,日整型){
//使用日期您的活动
}
...
}
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
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?
This is my Dialog fragment
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");
}
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
EDIT: not sure if it's important but parent activity is declared as:
public class EditSessionActivity extends FragmentActivity {
SOLUTION: thanks to Lecho user this is the way to do it
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);
}
}
...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);
}
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);
into this:
return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);
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
}
...
}
这篇关于获取日期使用dialogfragment日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!