显示片段中的对话框?
我有一些片段需要显示常规对话框。在这些对话框中,用户可以选择是/否答案,然后片段应该做出相应的行为。
现在, Fragment
类没有要重写的 onCreateDialog()
方法,所以我想我必须在包含 Activity< 的外部实现对话框/代码>。没关系,但是
Activity
需要以某种方式向片段报告所选的答案。我当然可以在这里使用回调模式,因此片段在 Activity 中使用侦听器类注册自身,并且 Activity 将通过该回调报告答案,或者类似的内容。
但这对于在片段中显示“简单”是-否对话框的简单任务来说似乎相当混乱。另外,这样我的 Fragment
就会变得不那么独立。
有没有一些更清洁的方法来做到这一点?
编辑:
这个问题的答案并没有真正详细解释应该如何使用 DialogFragments 来显示 Fragments 中的对话框。所以据我所知,要走的路是:
- 显示一个片段。
- 需要时,实例化 DialogFragment。
- 使用
.setTargetFragment()
将原始 Fragment 设置为此 DialogFragment 的目标。 - 使用原始片段中的 .show() 显示 DialogFragment。
- 当用户在此DialogFragment上选择某个选项时,通知原始Fragment有关此选择的信息(例如用户单击“yes”),您可以使用.getTarget()获取原始Fragment的引用。
- 关闭 DialogFragment。
I have some fragments that need to show a regular dialog. On these dialogs the user can choose a yes/no answer, and then the fragment should behave accordingly.
Now, the Fragment
class doesn't have an onCreateDialog()
method to override, so I guess I have to implement the dialogs outside, in the containing Activity
. It's ok, but then the Activity
needs to report back the chosen answer somehow to the fragment. I could of course use a callback pattern here, so the fragment registers itself at the Activity
with a listener class, and the Activity would report back the answer thru that, or something like that.
But this seems to be quite a big mess for a simple task as displaying a "simple" yes-no dialog in a fragment. Also, this way my Fragment
would be less self-contained.
Is there some cleaner way to do this?
Edit:
The answer to this question doesn't really explain in detail how one should use DialogFragments to display dialogs from Fragments. So AFAIK, the way to go is:
- Display a Fragment.
- When needed, instantiate a DialogFragment.
- Set the original Fragment as the target of this DialogFragment, with
.setTargetFragment()
. - Show the DialogFragment with .show() from the original Fragment.
- When the user chooses some option on this DialogFragment, notify the original Fragment about this selection (e.g. the user clicked 'yes'), you can get the reference of the original Fragment with .getTarget().
- Dismiss the DialogFragment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我必须谨慎地怀疑之前接受的答案,即使用 DialogFragment 是最佳选择。 DialogFragment 的预期(主要)目的似乎是显示对话框本身的片段,而不是显示具有要显示的对话框的片段。
我相信使用片段的活动在对话框和片段之间进行调解是更好的选择。
I must cautiously doubt the previously accepted answer that using a DialogFragment is the best option. The intended (primary) purpose of the DialogFragment seems to be to display fragments that are dialogs themselves, not to display fragments that have dialogs to display.
I believe that using the fragment's activity to mediate between the dialog and the fragment is the preferable option.
您应该使用 DialogFragment 代替。
You should use a DialogFragment instead.
以下是是/否 DialogFragment 的完整示例:
类:
启动对话框:
您还可以让该类实现 onClickListener 并使用它来代替嵌入式侦听器。
回调到 Activity
如果你想实现回调,这就是它的完成方式
在您的活动中:
和
回调类:
然后要从片段执行回调,您需要确保监听器像这样附加:
并且回调是这样执行的:
Here is a full example of a yes/no DialogFragment:
The class:
To start dialog:
You could also let the class implement onClickListener and use that instead of embedded listeners.
Callback to Activity
If you want to implement callback this is how it is done
In your activity:
and
The callback class:
Then to perform a callback from a fragment you need to make sure the listener is attached like this:
And a callback is performed like this:
对我来说,它是以下 -
MyFragment:
MyDialog:
Activity:
DialogFragment 布局:
正如名称
dialogfragment_heightfixhiddenbtn
所示,我只是想不出一种方法来修复底部按钮的高度被减半的问题尽管说了wrap_content
,所以我添加了一个隐藏按钮来“切成”两半。很抱歉被黑客入侵。For me, it was the following-
MyFragment:
MyDialog:
Activity:
DialogFragment layout:
As the name
dialogfragment_heightfixhiddenbtn
shows, I just couldn't figure out a way to fix that the bottom button's height was cut in half despite sayingwrap_content
, so I added a hidden button to be "cut" in half instead. Sorry for the hack.我自己也是一个初学者,老实说我找不到一个我可以理解或实现的令人满意的答案。
所以这里有一个外部链接,它确实帮助我实现了我想要的目标。它非常简单,也很容易遵循。
http://www.helloandroid.com/tutorials/ how-display-custom-dialog-your-android-application
这是我尝试使用代码实现的目标:
我有一个托管 Fragment 的 MainActivity。我希望在布局顶部出现一个对话框来请求用户输入,然后相应地处理输入。
查看屏幕截图
这是我的片段的 onCreateView 的外观
,希望它能对您有所帮助
,让我知道如果有任何混乱。 :)
I am a beginner myself and I honestly couldn't find a satisfactory answer that I could understand or implement.
So here's an external link that I really helped me achieved what I wanted. It's very straight forward and easy to follow as well.
http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application
THIS WHAT I TRIED TO ACHIEVE WITH THE CODE:
I have a MainActivity that hosts a Fragment. I wanted a dialog to appear on top of the layout to ask for user input and then process the input accordingly.
See a screenshot
Here's what the onCreateView of my fragment looks
I hope it will help you
Let me know if there's any confusion. :)
其中.test_dialog是xml自定义的
where .test_dialog is of xml custom