Fragment与Activity之间的互访

在activity中获取fragment中的数据

1.通过FragmentManager获取fragment布局

FragmantA fragmantA=(FragmantA) manager.findFragmentById(R.id.fragment1);

2.获取在fragment上的视图
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);

                   manager=getFragmentManager();

                   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();

         }

 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值