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

本文是小编为大家收集整理的关于如何将一个文本视图从一个片段更新到另一个片段?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个活动"主动脉",其中包含两个片段. fragment1具有递观和按钮.当用户按按钮时,EditText内部的文本将发送到Fragment2,该片段仅具有文本视图.但是我无法弄清楚如何将fragment1中的Edittext的文本发送到Fragment2中的TextView.如果有人可以帮忙?感谢.谢谢 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

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

推荐答案

快速简便的答案是从活动(如下所示)调用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();)
        }
    });
}

其他推荐答案

您可以通过多种方式进行操作.例如,通过Activity喜欢在官方文档.但是,我希望通过LocalBroadcastManager在中发送广播消息./stackoverflow.com/users/3114032/war-hero"> war_hero :

在发送片段(片段B-您的按钮段)

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

本文地址:https://www.itbaoku.cn/post/2091027.html

问题描述

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

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

推荐答案

The quick and easy answer is to call getView().findViewById() from the activity (as shown below). The more proper approach is to give your fragments an interface that gets and retrieves the text. Below is the code for the quick and easy method, but when you create a fragment in Android studio it shows you how to do the more proper interface approach.

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

其他推荐答案

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:

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

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