day05

activity向fragment传输数据

activity代码

public class MainActivity extends AppCompatActivity {
    private EditText ed;
    private Button submit;
    private LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ed = (EditText) findViewById(R.id.ed);
        submit = (Button) findViewById(R.id.submit);
        ll = (LinearLayout) findViewById(R.id.ll);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        BlankFragment blankFragment = new BlankFragment();
        transaction.add(R.id.ll,blankFragment);
        transaction.commit();
    }

    public void click(View view) {
        String string = ed.getText().toString();
        BlankFragment fragment = new BlankFragment();
        Bundle bundle = new Bundle();
        bundle.putString("key",string);
        fragment.setArguments(bundle);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.ll,fragment);
        transaction.commit();
    }
}

fragment代码

public class BlankFragment extends Fragment {
    private TextView tv;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
        tv = inflate.findViewById(R.id.tv);
        Bundle bundle = getArguments();
        if(bundle!=null){
            String key = bundle.getString("key");
            tv.setText(key);
        }

        return inflate;
    }

}

fragment传fragment

main

public class MainActivity extends AppCompatActivity {
    private BlankFragment fragment;
    private BlankFragment2 fragment2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        fragment = new BlankFragment();
        fragment2 = new BlankFragment2();
        transaction.add(R.id.frag1,fragment);
        transaction.add(R.id.frag2,fragment2);
        transaction.commit();
    }

    public void click(View view) {

        fragment.hahaha();
        Bundle bundle = fragment.getArguments();
        fragment2.setArguments(bundle);
        fragment2.tusi();
    }
}

fragment1

public class BlankFragment extends Fragment {

    private EditText et;
    private Button btn;
    public BlankFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_blank, container, false);


        et = (EditText) inflate.findViewById(R.id.et);
        btn = (Button) inflate.findViewById(R.id.btn);

        return inflate;
    }

    public void hahaha() {
        String s = et.getText().toString();
        Bundle bundle = new Bundle();
        bundle.putString("str",s);
        setArguments(bundle);
    }

}

fragment2

public class BlankFragment2 extends Fragment {
    private TextView tv;
    private static final String TAG = "BlankFragment2";
    public BlankFragment2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
        tv = inflate.findViewById(R.id.tv);

        return inflate;
    }
    public void tusi(){
        Bundle bundle = getArguments();

        if(bundle!=null){
            Log.i(TAG, bundle.toString());
            String str = bundle.getString("str");
            Log.i(TAG, str);
            tv.setText(str);
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值