目前已知的fragment的数据传输方式有三种:
三. 第三种
- 在活动内获取 fragment里面的控件信息,在活动中类似控件进行数据处理
- 需求:实现登陆页面,点击登陆后,数据会传递到下一个页面,采用碎片处理。
实现机制:
1) 在主活动中动态加载Fragment1
2) 在Fragment1中设置按钮点击事件,建立逻辑,如果进行跳转,则直接再加载一个碎片Fragment2.
3) 数据的处理。在碎片Fragment2中建立方法,传入数据, 使用bundle 来处理,通过setArguments(bundle)数据就放入 Fragment2的实例里面
public static Fragment2 newInstance(String value){
Fragment2 fragment2=new Fragment2();
//创建Bundle 传入数据
Bundle bundle=new Bundle();
bundle.putString("name", value);
//将参数与事例关联
fragment2.setArguments(bundle);
return fragment2;
}
4) Fragment2获取数据,可以在Activity建立起来后,在onActivityCreated 方法中进行处理 ,此时Fragment2的实例已包含数据,通过Bundle bundle = getArguments()
取出数据,然后对控件赋值。
补充:
动态加载Fragment的方式
//1. 获取碎片对象
Fragment1 fragment1=new Fragment1();
//2.获取管理器
FragmentManager fragmentManager = getSupportFragmentManager();
//3.获取事务
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//4.事务处理
beginTransaction.add(R.id.frameLayout, fragment1);
//5.事务提交
beginTransaction.commit();
示例代码
1.1 在src 中
1.1.1 Fragment1类
public class Fragment1 extends Fragment{
private TextView name;
private Button button1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment1, null);
//找控件
name = (TextView) view.findViewById(R.id.et_name);
button1 = (Button) view.findViewById(R.id.button1);
//设置点击事件
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//获取控件的相关内容
String name1=name.getText().toString();
if(TextUtils.isEmpty(name1)){
Toast.makeText(getActivity(), "用户名不能为空", 0).show();
}else{
//通过管理器打开碎片二
//进行数据传输
//获取管理器
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
//获取事务
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//获取碎片二的对象 并传入数据
Fragment2 fragment2=Fragment2.newInstance(name1);
//通过事务获取 参数
beginTransaction.add(R.id.frameLayout, fragment2);
//通过事务 对前面的碎片进行隐藏或移除,一般二选一或者加深背景掩饰都可以达到效果
beginTransaction.hide(Fragment1.this);//隐藏
beginTransaction.remove(Fragment1.this);//移除
//提交事务
beginTransaction.commit();
}
//获取控件信息
}
});
return view;
}
}
1.1.2 Fragment2类
public class Fragment2 extends Fragment{
private TextView tv_frag2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment2, null);
tv_frag2 = (TextView) view.findViewById(R.id.tv_frag2);
return view;
}
//创建活动后处理数据
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
//接收数据
Bundle bundle = getArguments();
String name=(String) bundle.get("name");
tv_frag2.setText(name);
}
//写方法提供碎片二新的事例 并获取数据
public static Fragment2 newInstance(String value){
Fragment2 fragment2=new Fragment2();
//创建Bundle 传入数据
Bundle bundle=new Bundle();
bundle.putString("name", value);
//将参数与事例关联
fragment2.setArguments(bundle);
return fragment2;
}
}
1.1.3 在main_activity 类中实现
/*
* 动态获取 碎片
* 1.实例化fragment
* 2.获取事务 v4 包下的 getSupportFragmentManager()管理器,并获取事务
* 3.通过事务给控件赋属性 一般将碎片赋给帧布局
* 4.事务的提交
*/
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1. 获取碎片对象
Fragment1 fragment1=new Fragment1();
//2.获取管理器
FragmentManager fragmentManager = getSupportFragmentManager();
//3.获取事务
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//4.事务处理
beginTransaction.add(R.id.frameLayout, fragment1);
//5.事务提交
beginTransaction.commit();
}
}
1.2 在res->layout中定义xml
1.2.1 fragment1.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:gravity="center"
android:background="#0f0"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="用户名"
android:textSize="20sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textSize="20sp" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="密 码"
android:textSize="20sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:inputType="textPassword"
android:textSize="20sp" >
</EditText>
</LinearLayout>
<Button
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆" />
</LinearLayout>
1.2.2 fragment2.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="#ff0"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_frag2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="用户名"
android:textSize="20sp" />
</LinearLayout>
1.2.3 activity_main.xml
<RelativeLayout 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:background="#FFA8A8"
>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>