Fragment 元素中的 Fragment onClick 方法

发布于 2024-12-22 06:20:41 字数 554 浏览 2 评论 0原文

我读了很多关于片段的文章,但我仍然对如何做什么感到困惑。

我有一个 MainActivity,它并排显示两个片段。在其中一个片段中,我有一个按钮,并在该按钮的片段布局 XML 中定义。

android:onClick="buttonClicked"

现在我想实现该方法

public void buttonClicked(View view)  

,我假设这必须在 FragmentA.java 而不是 MainActivity.java 中实现。但只有在 MainActivity.java 中实现该方法时它才有效。这是为什么?对我来说这没有意义。在蜂窝之前,属于一个活动的方法留在该活动中,现在在平板电脑上我将许多活动合并到一个 MainActivity 中,并且所有不同的方法都被合并?那么你在 FragmentA.java 中放了什么?如果您必须启动一个自己的活动,因为该应用程序在手持设备上运行,那么 onClick 方法不必位于 MainActivity 中,而是位于需要调用的 Activity 中,该怎么办?我现在很困惑...

I read quite some articles about fragments, but I am still confused about how to do what.

I have a MainActivity, which displays two fragments side by side. In one of the fragments I have a button and defined in the fragments layout XML for the button

android:onClick="buttonClicked"

Now I want to implement that method

public void buttonClicked(View view)  

I would have assumed that this has to be implemented in FragmentA.java and not in MainActivity.java. But it only works if that method is implemented in MainActivity.java. Why is that? To me that doesn't make sense. Pre Honeycomb a method belonging to one activity stayed in that activity, now on a tablet I am merging many activities to one MainActivity and all the different methods are merged? Whatever do you put for example in FragmentA.java then? What if you have to start you an own activity because this app runs on a handheld, then the onClick method has not to be in the MainActivity but in the Activity which needs to be called then. I am pretty confused at the moment...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小帐篷 2024-12-29 06:20:41

我不确定具体问题是什么,但这也许会有所帮助。

来自 Fragment 的 Android 文档:

您应该将每个片段设计为模块化且可重用的活动组件。也就是说,由于每个片段都通过自己的生命周期回调定义了自己的布局和自己的行为,因此您可以在多个活动中包含一个片段,因此您应该进行重用设计,并避免直接从另一个片段操作一个片段。

也就是说,您永远不应该从另一个片段操作一个片段;相反,这应该通过底层 Activity 来完成。阅读这篇文章中的“创建 Activity 的事件回调”部分 了解更多信息(这很重要!!)。

另一方面,如果您希望按钮在 Fragment 本身内执行操作(即,如果您希望通过 Button 单击来更改 TextView 的文本 在 Fragment 中),您应该在 Fragment 中实现此功能,而不是在 Activity 中(这是因为结果行为包含在 Fragment< /code> 与父级无关活动)。

发表评论,我可以澄清我的帖子是否令人困惑......我最近才开始理解 Fragment 自己:)。

I'm not sure what the specific problem is, but maybe this will help.

From the Android documentation on Fragments:

You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.

That is, you should never manipulate a fragment from another fragment; rather, this should be done through the underlying Activity. Read the "Creating event callbacks to the activity" section in this article for more information (it's important stuff!!).

On the other hand, if you want the button to perform an action within the Fragment itself (i.e. if you wanted a Button click to change the text of a TextView within the Fragment), you should implement this in the Fragment, not the Activity (this is because the resulting behavior is contained within the Fragment and has nothing to do with the parent Activity).

Leave a comment and I can clarify if my post is confusing... I only recently began to understand Fragment's myself :).

琉璃梦幻 2024-12-29 06:20:41

嗯,

我猜这与android上下文结构的层次结构有关。
Activity 是所有子视图的宿主,因此您可以说片段实际上正在使用其宿主的上下文。这就是为什么当您将 onClick 与片段系统一起使用时总是在片段的宿主活动中搜索它。

检查一下。
Android 开发者 onClick 属性说明

我还没有检查过但你可以进行测试。
通过在宿主活动中而不是在片段中提供实现,但在片段的布局文件上使用onClick。它应该调用父级的方法。

Well,

I guess it is related to hierarchy of android context structure.
Activity is host of all child views and hence you can say fragment is actually using its host's context.And that's why when you use onClick with fragment system always searches it in Host activity of fragment.

Check it on.
Android developer onClick attribute description

I haven't checked one thing but you could put a test.
By providing implementation in host activity rather than in fragment,but use onClick on layout file of fragment.It should call parent's method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文