问题描述
这关系到我的previous这里的问题:<一href=\"http://stackoverflow.com/questions/18061993/open-a-dialogfragment-from-within-a-customview/18062221?noredirect=1#18062221\">Open从CustomView 内DialogFragment
This is related to my previous question here: Open a DialogFragment from within a CustomView
我现在需要使用一个回调从我DialogFragment返回一个值。据我所知,这样的事情通常做的:
I now need to use a callback to return a value from my DialogFragment. I understand that something like this is commonly done:
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();
当你想要一个活动来实现和接收回调是这样的话。但是,如果我想的自定义内容做的(比如我在previous问题)?
That's the case when you want a Activity to implement and receive the callback. But, what if I want a custom view to do that (such as in my previous question)?
推荐答案
您做的东西─你创建一个界面像上面一样。你一直到该接口类型的变量的引用。那你有某种功能registerListener,需要一个侦听器对象,并将其存储以便以后可以调用它。
You do the same thing- you create an interface like above. You keep a reference to a variable of that interface type. Then you have some function registerListener that takes a listener object and stores it so you can call it later.
这篇关于返回回调(从CustomView内打开DialogFragment)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!