从datepicker使用dialogfragment获取date

我正在使用谷歌的例子,在我的应用程序中使用dialogfragment插入一个datepicker
http://developer.android.com/guide/topics/ui/controls/pickers.html

但我不知道如何获得date设置后(不是Java专家)。 对话框和datepicker运行正常,我可以logging该date是正确的设置,但是,我怎么能得到一个父活动执行的callback?

这是我的对话框片段

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);** } } 

…我从我的活动中调用对话…

 public void showDatePickerDialog(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getSupportFragmentManager(), "datePicker"); } 

什么是正确的方法来调用我的父代活动,而不是Log.w? 我想这是相关的传递一个callback作为参数或东西,但我发现在互联网上的大多数参考是关于以前的版本没有dialogfragments

编辑:不知道是否重要,但家长的活动声明为:

 public class EditSessionActivity extends FragmentActivity { 

解决scheme:感谢Lecho用户,这是做到这一点的方法

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 …

 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 )中实现这个接口并且改变这一行:

 return new DatePickerDialog(getActivity(), this, year, month, day); 

进入这个:

 return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day); 

然后你的活动应该是这样的:

 public class EditSessionActivity extends FragmentActivity implements DatePickerDialog.OnDateSetListener{ public void onDateSet(DatePicker view, int year, int month, int day) { //use date in your activity } ... } 

我简单的重写onDateSet在dateselect器创build在我的课,显示一个dateselect器。 见下面的代码:

  private OnClickListener onDateClicked() { return new OnClickListener() { @Override public void onClick(View v) { DialogFragment newFragment = new DatePickerFragment() { @Override public void onDateSet(DatePicker view, int year, int month, int day) { salePaymentCustomView.setBtDateText("" + day + "/" + month+1 + "/" + year); } }; newFragment.show(getActivity().getSupportFragmentManager(), "datePicker"); } }; } 

如果不将onDateSet方法移动到父活动,也可以解决该问题。 您只需告诉DatePickerFragment在哪里search视图:

 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) { // do some stuff for example write on log and update TextField on activity Log.w("DatePicker","Date = " + year); ((TextView) getActivity().findViewById(R.id.tf_date)).setText("Date = " + year); } } 

我也将EditText的目标转换为TextView 。 这样,您可以重用您的实现不同types的意见,不仅为EditText

我有一个更正上述答案。 而不是像这样返回DatePickerDialog

返回新的DatePickerDialog(getActivity(),(EditSessionActivity)getActivity(),年,月,日);

你应该以更通用的方式返回它

返回新的DatePickerDialog(getActivity(),(OnDateSetListener)getActivity(),年,月,日);

只要确保您的活动实现了OnDateSetListener接口并重写onDateSet函数

 @Override public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) 
 public void onDate(View view) { DialogFragment fragment = new SelectDateFragment(); fragment.show(getFragmentManager(), "Date Picker"); } // @SuppressLint("NewApi") class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { public Dialog onCreateDialog(Bundle savedInstanceState) { System.out.println("entrering on create dialog");; return new DatePickerDialog(getActivity(), this, mYear, mMonth, mDay);//it will return dialog setting date with mYera,MMonth and MDay } @Override public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) { System.out.println("year=" + year + "day=" + dayOfMonth + "month=" + monthOfYear); mYear=year; mMonth=monthOfYear; mDay=dayOfMonth; onPopulateSet(year, monthOfYear + 1, dayOfMonth); } // set the selected date in the edit text private void onPopulateSet(int year, int i, int dayOfMonth) { EditText et_setDate; et_setDate = (EditText) findViewById(R.id.register_et_dob);//register_et_dob:-id name of the edit text et_setDate.setText(dayOfMonth + "/" + i + "/" + year); System.out.println("enetring on populate Set"); } 

Leszek的答案是有效的,但是如果Dialog是从另一个片段开始的话。 在这种情况下,您需要在创build对话框的代码中使用setTargetFragment() ,并在对话框代码中使用getTargetFragment()

在创build对话框的代码中:

 DialogFragment dialogFragment = new YourDialogFragment(); dialogFragment.setTargetFragment(YourParentFragment.this, 0); dialogFragment.show(getFragmentManager(), "mydialog"); 

在DialogFragment代码中:

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { YourParentFragment parentFragment = (YourParentFragment) getTargetFragment(); // Manipulate the parent fragment // ... } }; // Get the initial date // ... return new DatePickerDialog(getActivity(), listener, year, month, day); } 

我有一个类似的问题,但我的dateselect器是从另一个片段内的对话框片段启动。 这使得callback变得非常困难,因为他们想要返回主活动,并且希望数据返回到前一个对话框片段。

最初,我将新的date值传递给主活动(使用OnDateSetListener),并使用启动dateselect器的对话框获取它们,但是当datepickerclosures时,没有在该对话框中触发的生命周期事件。

我得到的结果是在dateselect器onDismiss中,实例化一个新的对话框片段,调用一个设置的结果方法,然后启动它。 当然,为此,您需要确保在启动dateselect器时前一个片段已被解除。

这是调用dateselect器的dialogFragment

 public class NewTrialDialogFragment extends DialogFragment { public final String LOG_TAG = "NewTrialDialog Fragment"; Button startDateButton; Integer newYear, newMonth, newDay; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.dialog_new_trial, container, false); getDialog().setTitle("Dialog New Trial"); startDateButton = (Button) rootView.findViewById(R.id.button_start_date); startDateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDatePickerDialog(v); } }); //If you exit the datepicker without choosing anything, it returns zeros if (newYear != null && newMonth != null && newDay != null && newYear != 0) { startDateButton.setText(newYear + " " + newMonth + " " + newDay); }else{ startDateButton.setText("Select Start Date"); } return rootView; } public void showDatePickerDialog(View v) { TrialDatePickerDialog newDialog = new TrialDatePickerDialog(); newDialog.show(getActivity().getSupportFragmentManager(), "datePicker"); this.dismiss(); } public void setDates(int year, int month, int day){ newYear = year; newMonth = month; newDay = day; }} 

和dateselect器

 public class TrialDatePickerDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener { private final String LOG_TAG = "TrialDatePickerDialog"; int newYear, newMonth, newDay; @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 TrialDatePickerDialog and return it return new DatePickerDialog(getActivity(), this , year, month, day); } @Override public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); FragmentManager fm = getActivity().getSupportFragmentManager(); NewTrialDialogFragment newTrialDialogFragment = new NewTrialDialogFragment(); newTrialDialogFragment.setDates(newYear, newMonth, newDay); newTrialDialogFragment.show(fm, "new_trial_dialog"); } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { newYear = year; newMonth = monthOfYear; newDay = dayOfMonth; } }