Java 片段到片段编辑文本

Java 片段到片段编辑文本,java,android,android-fragments,Java,Android,Android Fragments,我想在单击按钮时将我的EditText输入从第一个片段传递到第二个片段上的textview。但触发按钮点击时,textview显示“空”值 第一段: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.first_frag, container, fa

我想在单击按钮时将我的EditText输入从第一个片段传递到第二个片段上的textview。但触发按钮点击时,textview显示“空”值

第一段:

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

    TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
    tv.setText(getArguments().getString("msg"));

    Button btn = (Button) v.findViewById(R.id.tryBtn);
    btn.setOnClickListener(new View.OnClickListener() {
                               @Override
                               public void onClick(View v) {
                                   EditText woo = (EditText)v.findViewById(R.id.editText);
                                   MainActivity.myBundle.putString("Try", String.valueOf(woo));
                                   switch (v.getId()) {
                                       case R.id.tryBtn:
                                      SecondFragment home = new SecondFragment();
                                           FragmentTransaction ft = getFragmentManager().beginTransaction();
                                           ft.replace(R.id.first_frag, home);
                                           ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
                                           ft.addToBackStack(null);
                                           ft.commit();
                                           break;
                                   }
                               }
                           }
    );

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

    TextView tv = (TextView) v.findViewById(R.id.tvFragSecond);
    TextView tvTry = (TextView) v.findViewById(R.id.tvTry);
    String myValue = (String) MainActivity.myBundle.get("Try");

    return v;
}
第二段:

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

    TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
    tv.setText(getArguments().getString("msg"));

    Button btn = (Button) v.findViewById(R.id.tryBtn);
    btn.setOnClickListener(new View.OnClickListener() {
                               @Override
                               public void onClick(View v) {
                                   EditText woo = (EditText)v.findViewById(R.id.editText);
                                   MainActivity.myBundle.putString("Try", String.valueOf(woo));
                                   switch (v.getId()) {
                                       case R.id.tryBtn:
                                      SecondFragment home = new SecondFragment();
                                           FragmentTransaction ft = getFragmentManager().beginTransaction();
                                           ft.replace(R.id.first_frag, home);
                                           ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
                                           ft.addToBackStack(null);
                                           ft.commit();
                                           break;
                                   }
                               }
                           }
    );

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

    TextView tv = (TextView) v.findViewById(R.id.tvFragSecond);
    TextView tvTry = (TextView) v.findViewById(R.id.tvTry);
    String myValue = (String) MainActivity.myBundle.get("Try");

    return v;
}
主要活动:

public static Bundle myBundle = new Bundle();

使用Bundle,显示下面的代码

  Fragment fragment = new SecondFragment();
  FragmentManager fm =getSupportFragmentManager();
  FragmentTransaction ft = fm.beginTransaction();
  ft.replace(R.id.frame_layout, fragment);
  ft.commit();

  Bundle bundle = new Bundle();
  bundle.putString("id", woo.getText().toString());
  fragment.setArguments(bundle);
Bundle bundle = this.getArguments();
      if (bundle != null) {
         String id  = bundle.getString("id");
      }
tv.setText(id);
并在
SecondFragment
中获得该值,如下面的代码所示

  Fragment fragment = new SecondFragment();
  FragmentManager fm =getSupportFragmentManager();
  FragmentTransaction ft = fm.beginTransaction();
  ft.replace(R.id.frame_layout, fragment);
  ft.commit();

  Bundle bundle = new Bundle();
  bundle.putString("id", woo.getText().toString());
  fragment.setArguments(bundle);
Bundle bundle = this.getArguments();
      if (bundle != null) {
         String id  = bundle.getString("id");
      }
tv.setText(id);

使用参数调用
newInstance
怎么样

SecondFragment.newInstance(/*your text*/)  //when you open your fragment

//in your Second Fragment
 public static SecondFragment newInstance(String data) {
    SecondFragment sFragment = new SecondFragment ();
    Bundle bundle = new Bundle();
    bundle.putString("data", data);
    sFragment.setArguments(bundle);
    return sFragment;
    }

关于你的声明:

但触发按钮点击时,textview显示“空”值

这不是从editText获取文本的方法

EditText woo = (EditText)v.findViewById(R.id.editText);
String.valueOf(woo)
试试这个:

woo.getText().toString()
关于在我使用的片段之间传递信息

它使用观察者模式:

您订阅了一个事件:

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onMessageEvent(MessageEvent event) {/* Do something */};
您发布事件:

EventBus.getDefault().post(new MessageEvent());
如果您同时拥有两个片段,这将起作用。 考虑到你的逻辑,你没有两个片段。所以我会做下一步:

您的片段应该调用您的活动,如:

mainActivity.onSentInputField(woo.getText().toString());
您的活动将负责使用正确的信息更改片段。这样,您的代码更有条理,片段在一个地方被更改。

试试这段代码

第一个片段

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

//        TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
//        tv.setText(getArguments().getString("msg"));


        final EditText woo = (EditText)v.findViewById(R.id.editText);

        Button btn = (Button) v.findViewById(R.id.tryBtn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SecondFragment home = new SecondFragment();

                Bundle bundle = new Bundle();
                bundle.putString("Try", woo.getText().toString());
                home.setArguments(bundle);

                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.first_frag, home);
                ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
                ft.addToBackStack(null);
                ft.commit();

            }
        });

        return v;
    }
第二段

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

    TextView tv = (TextView) v.findViewById(R.id.tvFragSecond);
    TextView tvTry = (TextView) v.findViewById(R.id.tvTry);

    Bundle arguments = getArguments();
    if (arguments != null) {
        String myValue  = arguments.getString("Try");
        tvTry.setText(myValue);
    }


    return v;
}
在MainActivity中删除这一行,最好不要使用静态绑定

public static Bundle myBundle = new Bundle();

您忘记添加这一行:bundle.putString(“Try”,woo.getText().toString());在你的密码里,对不起。但是我应该把SecondFragment.newInstance(/*您的文本*/)放在哪里,以及在“您的文本”中放什么,我应该把公共静态SecondFragment newInstance(字符串数据){SecondFragment SFFragment=new SecondFragment();Bundle Bundle=new Bundle();Bundle.putString(“数据”,数据);SFFragment.setArguments(Bundle);返回sFragment;}?我很抱歉。我是Android开发新手。第一行你应该放在这里
SecondFragment home=SecondFragment.newInstance(“在这里你可以放任何你想要的字符串或数据”)您在第二个片段中放入的其他内容您认为这是在两个片段之间传递数据的正确方式吗?非常感谢。这很有效。。如果我要在单击按钮时添加多个值呢?只需将其放入已创建的bundle中:bundle.putString(“secondTry”,woo2.getText().toString());然后从参数中获取:String myValue2=arguments.getString(“secondTry”);