如何从片段更改主要活动的数据 [英] How to change data of Main Activity from Fragment

查看:48
本文介绍了如何从片段更改主要活动的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主要活动中有片段1,片段2,片段3.主活动中有一个TextView,这些片段如何更改文本视图中的文本?

I Have fragment 1, fragment 2, fragment 3 which are in the Main Activity. there is a TextView in the main Activity, how can the fragments change the text in the text view?

我已经进行了强制转换(Actvity).getActivity,但是我不希望我的应用在更改活动"时出现错误

I already did the casting (Actvity).getActivity but i don't want my app to have error when changing Activities

推荐答案

在活动中编写诸如 setTextViewText(String str)之类的setter方法,并从片段中调用诸如((YourActivity)getActivity()).setTexrViewText(str)

Write a setter method like setTextViewText(String str) in the activity and from fragment call the method like ((YourActivity)getActivity()).setTexrViewText(str)

这篇关于如何从片段更改主要活动的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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