Fragment 元素中的 Fragment onClick 方法
我读了很多关于片段的文章,但我仍然对如何做什么感到困惑。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定具体问题是什么,但这也许会有所帮助。
来自
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
Fragment
s: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 aButton
click to change the text of aTextView
within the Fragment), you should implement this in theFragment
, not theActivity
(this is because the resulting behavior is contained within theFragment
and has nothing to do with the parentActivity
).Leave a comment and I can clarify if my post is confusing... I only recently began to understand
Fragment
's myself :).嗯,
我猜这与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.