1.通过FragmentManager获取fragment布局
FragmantA fragmantA=(FragmantA) manager.findFragmentById(R.id.fragment1);
View fragmanView=fragmantA.getView();
3.获取视图上的控件
TextViewtextView=(TextView) fragmanView.findViewById(R.id.frag_tv);
4.更改控件上的数据textView.setText("hhehehehehheh");
================================================================
在fragment中获取activity中的数据1. 在Fragment中可以通过getActivity的方法来获取activity中的布局
2.调用Activity中的findViewByBd来获取布局中的控件
TextViewtextView=(TextView) getActivity().findViewById(R.id.main_tv);
3. 改变数据
textView.setText("呵呵呵呵呵呵呵");
====================================================
代码详解:
布局:
主布局:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<fragment
android:id="@+id/fragment1"
android:name="com.qianfeng.lx_betweenactivityandfragment1.FragmantA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10"/>
<TextView
android:id="@+id/main_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:text="TextView" />
<Button
android:id="@+id/main_bt"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:text="Button" />
</LinearLayout>
子布局(fragment使用的布局)
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/frag_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.23"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.04"
android:text="改变activity中的内容" />
</LinearLayout>
详细代码:
public classMainActivity extends Activity {
private Button button;
private FragmentManagermanager;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.main_bt);
button.setOnClickListener(newOnClickListener() {
@Override
public voidonClick(View v) {
// TODOAuto-generated method stub
在activity中获取fragment中的控件
1.通过manager来获取到fragment布局
2.获取在fragment上的视图
3.获取视图上的控件
4.更改控件上的数据
//获得Fragment布局通过id
FragmantAfragmantA=(FragmantA) manager.findFragmentById(R.id.fragment1);
//获得Fragment的视图
ViewfragmanView=fragmantA.getView();
TextViewtextView=(TextView) fragmanView.findViewById(R.id.frag_tv);
textView.setText("hhehehehehheh");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menumenu) {
// Inflate the menu; thisadds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
}
public classFragmantA extends Fragment {
private Button button;
@Override
public void onCreate(BundlesavedInstanceState) {
// TODO Auto-generated methodstub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflaterinflater, ViewGroup container,
BundlesavedInstanceState) {
// TODO Auto-generated methodstub
Viewview=inflater.inflate(R.layout.fragment_itema, null);
button=(Button)view.findViewById(R.id.button1);
button.setOnClickListener(newOnClickListener() {
@Override
public voidonClick(View v) {
// TODOAuto-generated method stub
TextViewtextView=(TextView) getActivity().findViewById(R.id.main_tv);
textView.setText("呵呵呵呵呵呵呵呵而后");
}
});
return view;
}
@Override
public void onPause() {
// TODO Auto-generated methodstub
super.onPause();
}