2017-08-04 179 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回答更新 –