如何将textview从一个片段更新到另一个片段

时间:2017-05-16 18:07:30

标签: android android-fragments fragment

我有一项活动" MainActivity"包含两个片段。 Fragment1有一个EditText和一个按钮。当用户按下按钮时,EditText内的文本将被发送到只有TextView的Fragment2。但我无法弄清楚如何将文本从Fragment1中的EditText发送到Fragment2中的TextView。如果有人可以帮忙?赞赏。谢谢 片段1

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

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

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);
    }
}

2 个答案:

答案 0 :(得分:0)

快速简单的答案是从活动中调用getView()。findViewById()(如下所示)。更合适的方法是为您的片段提供获取和检索文本的接口。下面是快速简便方法的代码,但是当您在Android studio中创建片段时,它会向您展示如何更合适的界面方法。

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();

    EditText et = (EditText) ft1.getView().findViewById(YOUR_EDITTEXT_ID);
    Button button = (Button) ft1.getView().findViewById(YOUR_BUTTON_ID);
    TextView tv = (TextView) ft2.getView().findViewById(YOUR_TEXTVIEW_ID);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tv.setText(et.getText();)
        }
    });
}

答案 1 :(得分:0)

你可以通过几种方式来做到这一点。例如,通过Official Docs中的Activity。但是,我更喜欢通过LocalBroadcastManager发送广播消息,如thiswar_Hero回答:

  

发送片段(片段B - 你的ButtonFragment)

public class FragmentB {

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

在收到片段的消息中(片段A - 你的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");
      }
    };
}

不要忘记取消注册接收者:

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