您的位置:首页 > 其它

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

2016-04-05 20:22 363 查看
[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();

}

});

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: