简体   繁体   English

如何从Android中的片段访问主要活动按钮

[英]how to access main activity button from fragment in android

In main activity I have 5 buttons an I have no buttons in fragments how to access those main activity buttons from fragments. 在主要活动中,我有5个按钮,在片段中没有按钮,如何从片段中访问那些主要活动按钮。 I have total 5 fragments class. 我总共有5个片段类。

If you want to access the button of YourActivity in the fragment, then you can use getActivity() reference. 如果要访问片段中的YourActivity按钮,则可以使用getActivity()引用。

Make your buttons as public in main activity, 在主要活动中将您的按钮设为公开

public Button mybtn;

Then in your fragment, Access it by 然后在您的片段中,通过

((YourActivity)getActivity()).mybtn

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM