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
- Android的Activity获取fragment和fragment的组件
- 获取当前点击的Activity和fragment
- Fragment和Activity的控件获取
- Android中的Fragment ---- 04(Activity和Fragment之间的通信)
- Android中的Fragment ---- 05(Activity和Fragment之间的通信)
- Android - 总结Activity与Fragment打开的时候获取组件的大小
- Android Activity和Fragment的转场动画
- android fragment和activity的区别
- Android Activity和Fragment的生命周期图
- Android Fragment和Activity的交互介绍
- Android Activity和Fragment的生命周期
- Android Activity和Fragment的转场动画
- Android Activity和Fragment的转场动画
- android 中fragment和activity的区别?
- Android Activity和Fragment的转场动画
- android fragment和activity的区别
- Android Activity和Fragment的转场动画
- Android Activity和Fragment的转场动画
- JQuery中$.ajax()方法参数详解
- IDR、CRA、BLA、RASL、RADL、closed-gop、open-gop
- SourceInsight 字体设置
- 任务栏网络连接中找不到无线网络连接
- 关于Express4版本不支持express.createServer()
- Android的Activity获取fragment和fragment的组件
- 3.15、循环的嵌套
- 使用C#开发ActiveX控件
- window.location.search
- Android ListPreference的用法
- web端兼容性问题汇总(css篇)
- java中为什么要引入内部类
- 【JavaScript】利用sort()函数与文件碎片实现表格的前端排序,兼容IE6原生态
- 进程自我创建---挂起进程方法