这与我之前的问题有关:Open a DialogFragment from within a CustomView
我现在需要使用回调从我的 DialogFragment 中返回一个值。我知道通常会这样做:
public class MyDialogFragment extends DialogFragment {
public interface onMultipleSelectionFragmentCloseListener {
public void onMultipleSelectionFragmentOkay();
}
onMultipleSelectionFragmentCloseListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (onMultipleSelectionFragmentCloseListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement onMultipleSelectionFragmentCloseListener");
}
}
....
// to use it
mListener.onMultipleSelectionFragmentOkay();
当您希望 Activity 实现并接收回调时就是这种情况。但是,如果我想要一个自定义视图来做到这一点(例如在我之前的问题中)怎么办?