【问题标题】:How to SetText of One Activity/Fragment from another one如何从另一个活动/片段中设置一个活动/片段的文本
【发布时间】:2016-08-15 22:34:22
【问题描述】:

我想从 Fragment_A 更改 Fragment_B 的文本。我怎样才能做到这一点?

类似的东西。

片段_A

ola = (TextView) v.findViewById(R.id.textView12);
ola2 = (TextView) (FRAGMENT_B).findViewById(R.id.textView2);

AsyncTask(ola,ola2).execute //this should set text on Fragment_A and Fragment_B

片段 A:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_second,container,false);


    ola = (TextView) v.findViewById(R.id.textView12);

    Asynctask(ola).execute

    return v;
}

片段 B:

public class Fragmento_B extends android.support.v4.app.Fragment{

    public TextView ola2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_third,container,false);

        ola2 = (TextView) v.findViewById(R.id.textView2);

        Asynctask(ola2).execute
        return v;
    }
}

【问题讨论】:

  • 使用参数启动片段 B,然后在片段 B 中获取参数,然后从参数中获取您的值,然后从您的值中获取 setText。

标签: java android android-fragments android-asynctask settext


【解决方案1】:

您面临的问题是什么?它是显示某种错误还是没有将文本更改为您的或任何内容?

【讨论】:

  • 我的代码运行良好,但我调用了 AsyncTask.execute 两次。我只想运行一次并从 fragment_A 更新 fragment_B 文本视图。这样我就不需要在 fragment_B 中运行 AsyncTask.execute。
  • 我相信你只需要将ola2的引用传递给片段A,然后在ola2视图上调用setText方法即可。我认为不需要 AsyncTask
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多