我如何将一个文本视图从一个片段更新到另一个 [英] How can I update a textview from one fragment to another

查看:70
本文介绍了我如何将一个文本视图从一个片段更新到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动"MainActivity",其中包含两个片段. Fragment1有一个EditText和一个按钮.当用户按下按钮时,EditText中的文本将被发送到仅具有TextView的Fragment2.但是我不太清楚如何将文本从Fragment1中的EditText发送到Fragment2中的TextView.如果有人可以帮忙?感激不尽谢谢 片段1

I have an activity "MainActivity" that contains two fragments. Fragment1 has a EditText and a button. When the user presses the button the text inside the EditText will be sent to Fragment2 which only has a TextView. But I can't quite figureout how to send the text from EditText in Fragment1 to TextView in Fragment2. If someone could help? Appreciated. Thanks Fragment1

package org.pctechtips.myfragmentapp;


/**
 * Created by george on 5/16/17.
 */

public class ButtonFragment extends Fragment {

    Button bt;
    EditText editText;
    TextView txtView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.button_fragment,
                container, false);

        txtView = (TextView) view.findViewById(R.id.textView_fragment1);
        editText = (EditText) view.findViewById(R.id.editText_fragment1);


        bt = (Button) view.findViewById(R.id.button_fragment1);
        bt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonClicked(v);
            }
        });

        return view;
    }

    public void buttonClicked (View view) {
        .onButtonClick(edittext.getText().toString());
    }

}

MainActivity

MainActivity

package org.pctechtips.myfragmentapp;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*fragments 1 section*/
        FragmentManager fm1 = getFragmentManager();
        FragmentTransaction ft1 = fm1.beginTransaction();
        ButtonFragment bf = new ButtonFragment();
        ft1.add(R.id.button_fragment1, bf);
        ft1.commit();

        /*Fragment 2 section*/
        FragmentManager fm2 = getFragmentManager();
        FragmentTransaction ft2 = fm2.beginTransaction();
        TextviewFragment tf = new TextviewFragment();
        ft2.add(R.id.textView_fragment2, tf);
        ft2.commit();

        }

}

Fragment2

Fragment2

public class TextviewFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.text_fragment, container, false);
    }
}

推荐答案

您可以通过几种方法来做到这一点.例如,通过Activity,例如官方文档.但是,我更喜欢通过LocalBroadcastManager发送广播消息,例如war_Hero :

You can do it by several ways. For example via Activity like in Official Docs. But, I'm prefer send broadcast messages via LocalBroadcastManager like in this answer of war_Hero:

在发送片段中(片段B-您的ButtonFragment)

In Sending Fragment(Fragment B - Your ButtonFragment)

public class FragmentB {

    private void sendMessage() {
      Intent intent = new Intent("custom-event-name");
      intent.putExtra("message", "your message");
      LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
 }

在要接收的消息中,片段(FRAGMENT A-您的TextviewFragment)

And in the Message to be Received Fragment(FRAGMENT A - Your TextviewFragment)

  public class FragmentA {
    @Override
    public void onCreate(Bundle savedInstanceState) {

      ...

      // Register receiver
      LocalBroadcastManager.getInstance(this).registerReceiver(receiver,
          new IntentFilter("custom-event-name"));
    }

//    This will be called whenever an Intent with an action named "custom-event-name" is broadcasted.
    private BroadcastReceiver receiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("message");
      }
    };
}

别忘了取消注册接收者:

And don't forget to unregister receiver:

protected void onPause() {
  super.onPause();
  LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
}

这篇关于我如何将一个文本视图从一个片段更新到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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