fragment2中获取fragment1布局,找到控件设置f1里边的内容

[main]

<fragment
        android:id="@+id/ll1"
        android:layout_weight="1"
        android:name="com.bwei.fragment_intance.F1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
       >
    </fragment>

    <fragment
        android:id="@+id/ll2"
        android:name="com.bwei.fragment_intance.F2"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
       >
    </fragment>
[f1]

   <TextView
        android:id="@+id/tv_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="页面一" />

[f2]

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="页面二" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吐司" />



【f2】/**


public class F2 extends Fragment {
    private View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        view = View.inflate(getActivity(), R.layout.f2, null);
        return view;}
    // 所依附的activity加载完成后会执行的方法
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO
        super.onActivityCreated(savedInstanceState);
        Button button = (Button) view.findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {    @Override
            public void onClick(View v) {
                // 通过id,得到静态配置的fragment
                F1 f1 = (F1) getFragmentManager().findFragmentById(R.id.ll1);
                // 得到f1显示的布局文件
                View f1_view = f1.getView();
                // 通过布局文件,由控件id得到想要的控件
                TextView tv_1 = (TextView) f1_view.findViewById(R.id.tv_1);

                String str = tv_1.getText().toString().trim();
                Toast.makeText(getActivity(), str, 0).show();
            }
        });
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值