我试图更改Fragment中的TextView值,但未清除旧值。 价值与旧价值重叠。 片段中的代码- 活动中的代码- 片段xml文件- 通过使用下面的代码尝试过,但是不起作用。 请帮助...。在此先感谢...! ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我有一个名为SpeedometerFragment
的片段,它有一个相应的 XML 布局fragment_speedometer
然后我创建了一个名为fragment_distance
的独立布局。 没有与此 XML 文件关联的活动。
我想中一个TextView“distanceText”的值更改fragment_distance
XML文件从一个函数调用SpeedometerFragment
。
fragment_distance
被传递到我的主要活动中的 frameLayout。
我曾尝试以下内的onCreateView SpeedometerFragment
但到TextView的没有变化:
速度计片段
LayoutInflater inflater1 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater1.inflate(R.layout.fragment_distance, container, false);
TextView dis = (TextView) v.findViewById(R.id.distanceText);
dis.setText("Test");
我想出了一个解决方案。 我在我原来的问题中把它复杂化了,所以不需要膨胀任何东西,也不需要单独的fragment_distance
XML 文件
我在我的主要活动中创建了一个public static TextView distance
。
然后在我的片段中,当距离的值发生变化时,我使用Eye_Tracking_Main.distance.setText(new DecimalFormat("#.#").format(distance) + " Km");
设置 TextView 的值
在我的主要活动中,我只是使用distance=findViewById(R.id.distancemain);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.