2017-08-04 178 views
1

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

第一块碎片:

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

MainActivity:

public static Bundle myBundle = new Bundle(); 

回答

0

试试这个代码,

第一块碎片

@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(); 
+0

非常感谢。这工作完美..如果我要添加多个值按钮点击? – randolfrojo11

+0

只是把它放在已创建的包中: bundle.putString(“secondTry”,woo2.getText()。toString());然后从参数中得到它:String myValue2 = arguments.getString(“secondTry”); –

0

使用捆绑为,显示下面的代码;

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

并得到这个值在SecondFragment像下面的代码;

Bundle bundle = this.getArguments(); 
     if (bundle != null) { 
     String id = bundle.getString("id"); 
     } 
tv.setText(id); 
1

那么用参数调用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; 
    } 
+0

您忘记了添加此行:bundle.putString(“Try”,woo.getText()。toString());在你的代码中。 –

+0

@HareshChhelana对。谢谢=) –

+0

对不起。但我在哪里把SecondFragment.newInstance(/ *你的文本* /)和什么放在“你的文本”里面,我也把公共静态的片段SecondFragment newInstance(String data){SecondScragment sFragment = new SecondFragment() ; Bundle bundle = new Bundle(); bundle.putString(“data”,data); sFragment.setArguments(bundle); return sFragment; } ? 我很抱歉。我是Android开发新手。 – randolfrojo11

0

关于您的语句:

但是TextView中显示,当点击按钮触发 “空” 值。

这不是从EDITTEXT得到一个文本

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

试试这个方式:

woo.getText().toString() 

关于我用EventBus

片段之间传递信息,它使用观察员模式:

您订阅的事件:

@Subscribe(threadMode = ThreadMode.MAIN) 
public void onMessageEvent(MessageEvent event) {/* Do something */}; 

您发布事件:

EventBus.getDefault().post(new MessageEvent()); 

如果您在同一时间有两个片段,将工作。 考虑到你的逻辑,你没有这两个片段。所以我会做下一个:

你的片段应该打电话给你的活动,如:

mainActivity.onSentInputField(woo.getText().toString()); 

而且你的活动将负责用正确的信息改变片段。这样你的代码更加有组织,碎片在一个地方被改变。

+0

你认为这是正确的方法在两个片段之间传递数据? –

+0

@HareshChhelana回答更新 –