第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過 Fragment 傳遞數(shù)據(jù)

通過 Fragment 傳遞數(shù)據(jù)

森林海 2021-06-21 21:01:20
我的應(yīng)用程序很簡單,只是我有一個FragmentA和FragmentB第一個有一個按鈕通過第二個片段傳遞字符串數(shù)據(jù)(“嗨,我是 A”),第二個片段有一個文本來顯示此消息我的問題是為什么我的“ModifyTxt”方法不起作用?public class newFragmentA extends Fragment{    Button button;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {        return inflater.inflate(R.layout.newfragmenta_layout,container,false);    }    newFragmentB newfragmentB;    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        button = getActivity().findViewById(R.id.button);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                newfragmentB.ModifyTxT("Hi I'm A");            }        });       }}這是我的第二個 Fragment(FragmentB)public class newFragmentB extends Fragment {    TextView textView;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {        return inflater.inflate(R.layout.newfragmentb_layout,container,false);    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        textView = getActivity().findViewById(R.id.TV);    }    public void ModifyTxT(String string){        textView.setText(string); }}
查看完整描述

2 回答

?
慕桂英4014372

TA貢獻1871條經(jīng)驗 獲得超13個贊

試試這個:


public class FragmentB extends Fragment {

  // ...


  public static FragmentB newInstance() {

        return new FragmentB();

    }

}

在片段A中:


button.setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View v) {

    FragmentB newFragment = FragmentB.newInstance();

    newFragment.modifyTxT("Hi I'm A");

  }

});

注意編碼規(guī)則:類名首字母大寫,方法首字母小寫...


查看完整回答
反對 回復 2021-06-23
?
慕慕森

TA貢獻1856條經(jīng)驗 獲得超17個贊

更喜歡使用接口進行通信 Fragments


public class FragmentA extends Fragment{

    public interface MyCallback{

        void modifyTxT(String text); // method naming convention start with small letter.

    }


    @Override

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        button = getActivity().findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                if (newfragmentB instanceof MyCallback) {

                    ((MyCallback)newfragmentB).modifyTxT("Hi I'm A");

                }

            }

        });



    }


}


public  class FragmentB extends Fragment implements FragmentA.MyCallback{


    public static FragmentB getInstance(){

        return new FragmentB();


    }


    @Override

    public void modifyTxT() {


    }

}


查看完整回答
反對 回復 2021-06-23
  • 2 回答
  • 0 關(guān)注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號