Android的Activity获取fragment和fragment的组件

来源:互联网 发布:军休所知乎 编辑:程序博客网 时间:2024/06/14 21:51

今天讲一下Android的Activity获取fragment和fragment的组件:

一.获取fragment:
当fragment被提交之后,即可通过以下两种方法获取fragment:
1.findFragmentById();
2.findFragmentByTag(String tag);
【fragmentTransaction.commit()提交fragment是异步处理的】

二.获取fragment的组件:
1.重写onStart()方法或onResume()【不能写在onCreate()中,因为该方法被调用时还无法相互获取对方组件】
2.直接通过findViewById()获取fragment的组件

看一下运行截图:
这里写图片描述

这里写图片描述

MainActivity.java:
注释写在程序中了

package activity.wyc.com.fragmentdemo0305;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import com.vrinux.fragment.Fragment01;import com.vrinux.fragment.Fragment02;public class MainActivity extends ActionBarActivity {    //private String TAG = "MainActivity";    private Button btnObj;    private FragmentManager fragmentManager = getFragmentManager();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Fragment01 fragment01 = new Fragment01();        Fragment02 fragment02 = new Fragment02();        FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();        /**         * fragmentTransaction.replace(R.id.fm01id,fragment01,"fragment01Tag");         * R.id.fm01id 既是是加载fragment的容器的id,当加载后也既是所加载fragment的id         * "fragment01Tag"是对fragment01的标记         */        fragmentTransaction.replace(R.id.fm01id,fragment01,"fragment01Tag");        fragmentTransaction.replace(R.id.fm02id,fragment02,"fragment02Tag");        fragmentTransaction.commit();        /**         * 当fragment被提交之后,即可通过以下两种方法获取fragment;【fragmentTransaction.commit()提交fragment是异步处理的,所以获取fragment时要注意】         * Fragment01 fragment01 = (Fragment01)fragmentManagerObj.findFragmentByTag("fragment01Tag");         *     或         * Fragment01 fragment01 = (Fragment01)fragmentManagerObj.findFragmentById(R.id.fm01id);         */    }    /**     * 重写onStart()方法,因为从fragment的生命周期可以知道当Activity的onCreate(Bundle savedInstanceState)中还无法获取fragment的布局的组件     */    @Override    protected void onStart() {        super.onStart();        btnObj = (Button)findViewById(R.id.btnid);        btnObj.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                /**                 * 可以直接通过findViewById()获取fragment的组件,因为fragment本身就是Activity的一部分(“碎片”/“片段”);                 * 因为Activity和fragment要从fragment的onActivityCreate()生命周期方法之后才能相互获取对方布局中的组件,                 *      所以在fragment中获取Activity的组件最早只能在onActivityCreate()中获取,而Activity最早只能在onStart()中获取;                 */                EditText fm01etObj =(EditText)findViewById(R.id.fm01etid);                String str = fm01etObj.getText().toString();                TextView fm02tvObj =(TextView)findViewById(R.id.fm02tvid);                fm02tvObj.setText(str);            }        });    }}

Fragment01.java:

package com.vrinux.fragment;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import activity.wyc.com.fragmentdemo0305.R;/** * Created by yc on 2015/3/5. */public class Fragment01 extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment01layout, null);        return view;    }}

Fragment02.java:

package com.vrinux.fragment;import android.app.Fragment;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import activity.wyc.com.fragmentdemo0305.R;/** * Created by yc on 2015/3/5. */public class Fragment02 extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment02layout, null);        return view;    }}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    android:orientation="vertical"    tools:context=".MainActivity">    <FrameLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/fm01id"></FrameLayout>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20sp"        android:id="@+id/btnid"        android:text="将fragment01的EditText的值传给fragment02的textview"        />    <FrameLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/fm02id"></FrameLayout></LinearLayout>

fragment01layout.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#00ffff"    android:orientation="vertical"    >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20sp"        android:text="我是fragment01"/>    <EditText        android:layout_width="200dp"        android:layout_height="wrap_content"        android:textSize="30sp"        android:id="@+id/fm01etid"/></LinearLayout>

fragment02layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent" android:layout_height="match_parent"    android:background="#ffff00"    android:orientation="vertical"    >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="我是fragment02"        android:textSize="20sp"        />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="30sp"        android:id="@+id/fm02tvid"/></LinearLayout>
0 0