片段和DialogFragment之间的通信 [英] Communication between Fragment and DialogFragment

查看:347
本文介绍了片段和DialogFragment之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个片段 A 对话框使用的 DialogFragment API

I'm trying to make a Fragment to show a Dialog using DialogFragment API.

我的对话框中只有一个的EditText 看法,我想通过它的串写回片段

My dialog only has an EditText view and I want to pass the string wrote on it back to the Fragment.

我能够显示来自片段的对话框,但我不能够检索从输入字段后面的字符串,以我的片段

I'm able to show the dialog from the Fragment, but I'm not able to retrieve the string from the input field back to my Fragment.

我想两种方式来创建一个对话框:覆盖 DialogFragment.onCreateView 方法和写作的整个对话的视图,并覆盖 DialogFragment.onCreateDialog 和使用 AlertDialog.Builder ,使其更容易编写按钮的行为。

I tried both ways to create a dialog: overwriting the DialogFragment.onCreateView method and writing the entire dialog's view and overwriting the DialogFragment.onCreateDialog and using the AlertDialog.Builder to make it easier to write the buttons behavior.

有关对话框传递活动回到对话的主机约的主机是永远的活动,从来没有一个片段。我试着概括为使用它在我的情况下的行为,但我不能。

The Android documentation about dialogs passing events back to dialog's host is about host being always an Activity and never a Fragment. I tried to generalize the behavior for use it on my case, but I cannot.

我尝试使用 DialogFragment.onAttach 回调以获取活动参考和使用的<$接口C $ C>片段和 DialogFrament ,它工作正常,但我不能检索片段活动,使其工作。

I tried using the DialogFragment.onAttach callback to get the Activity reference and use an interface between the Fragment and the DialogFrament and it works fine, but I cannot retrieve the Fragment from the Activity to make it work.

任何人可以帮助一个简单的例子?

Can anybody help with a brief example?

在此先感谢

推荐答案

您可以使用<一个href="http://developer.android.com/reference/android/app/Fragment.html#setTargetFragment%28android.app.Fragment,%20int%29"><$c$c>setTargetFragment和<一href="http://developer.android.com/reference/android/app/Fragment.html#getTargetFragment%28%29"><$c$c>getTargetFragment 片段的方法。您可以设置片段 DialogFragment 的目标,然后检索参考它在 DialogFragment

You can use the setTargetFragment and getTargetFragment methods of Fragment. You can set your Fragment as the target of the DialogFragment and then retrieve a reference to it from within the DialogFragment.

这篇关于片段和DialogFragment之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆