从Dialogfragment中的Timepicker返回值到调用片段

时间:2014-07-28 18:29:21

标签: android android-fragments android-dialogfragment timepicker

我正在尝试从片段中打开Dialogfragment。 dialogFragment有一个Timepicker,我想将所选的小时和分钟从timepicker返回到调用片段。我怎样才能做到这一点?我从问题中尝试了下面的内容,但无法使其正常工作。有人可以给下面的解决方案的伪代码(显示在最后)

我的代码显示片段中的Dialog:

TimePickerFragment timepicker = TimePickerFragment.newInstance(1);
timepicker.show(getFragmentManager(), "timepickerfrag");

Dialogfragment代码:

public class TimePickerFragment extends DisplayDialogFragment implements     View.OnClickListener {
private static final String ARG_PARAM1 = "param1";
private int mParam1;
private TimePicker time;
private Button bt1, bt2;
private TimePicker tm;

public static TimePickerFragment newInstance(int param1) {
TimePickerFragment fragment = new TimePickerFragment();
fragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}

public TimePickerFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getInt(ARG_PARAM1);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_time_picker_scheduler, container, false);
bt1 = (Button) view.findViewById(R.id.sched_time_pck_btn_ok);
bt2 = (Button) view.findViewById(R.id.sched_time_pck_btn_cancel);
tm = (TimePicker) view.findViewById(R.id.sched_dialog_time_picker);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
return view;
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sched_time_pck_btn_cancel:
dismiss();
break;
case R.id.sched_time_pck_btn_ok:
dismiss();
break;
}
}

}

Receive result from DialogFragment

在显示对话框的位置使用myDialogFragment.setTargetFragment(this,MY_REQUEST_CODE),然后在对话框完成后,您可以从中调用getTargetFragment()。onActivityResult(getTargetRequestCode(),... ),并在包含片段中实现onActivityResult()。 这似乎是对onActivityResult()的滥用,特别是因为它根本不涉及活动。但我已经看到它被官方谷歌人推荐,甚至可能在api演示中。我认为这是为g / setTargetFragment()添加的内容。

1 个答案:

答案 0 :(得分:2)

这应该有所帮助。将其添加到您正在显示对话框的Fragment中。

// {@link CustomFragment} - current fragment which invokes dialog.
// {@link CustomDialogFragment} - DialogFragment implementation to show.
// REQUEST_GET_DATE - Request code for call.
CustomDialogFragment dialog = new CustomDialogFragment(getActivity());
dialog.setTargetFragment(CustomFragment.this, REQUEST_GET_DATE);
dialog.show(getActivity().getSupportFragmentManager(), "Dialog");

// Result handling
// REQUEST_GET_DATE - Constant int for request
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == Activity.RESULT_OK) {
    switch (requestCode) {
      case REQUEST_GET_DATE:
        String extra = data.getStringExtra(DateTimeDialogFragment.EXTRA_DATE);
        // Handle response here..
        break;
    }
  }
}


// Inside CustomDialogFragment::onCreateDialog(Bundle savedInstanceState)
public Dialog onCreateDialog(Bundle savedInstanceState) {
  // Use the Builder class for convenient dialog construction
  Builder builder = new AlertDialog.Builder(mActivity);

  // Set the layout for the dialog
  builder.setView(mView);
  builder
    .setMessage("Title")
    .setPositiveButton("Set",
      new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          // User ok the dialog
          // Important part!
          // -----
          Intent intent = getActivity().getIntent();
          intent.putExtra(EXTRA_DATE, getDateTime());
          getTargetFragment().onActivityResult(CustomFragment.REQUEST_GET_DATE, Activity.RESULT_OK, intent);
          // -----
          getDialog().hide();
        }
      })
      .setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
            getDialog().cancel();
        }
      });
  // Create the AlertDialog object and return it
  return builder.create();
}