通过bundle对象实现Fragment之间的传值
来源:互联网 发布:淘宝免单可信吗 编辑:程序博客网 时间:2024/06/06 14:24
这里我们介绍一下使用bundle对象进行Fragment传值,比较简单我们直接看代码吧,都有注释的。
MainActivity:
package org.mobiletrain.fragment_demo07;import android.os.Bundle;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.view.Menu;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {RightFragment fragment;FragmentManager manager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);fragment = new RightFragment();manager = getFragmentManager();FragmentTransaction transaction = manager.beginTransaction();//通过bundle对象向Fragment传值Bundle bundle = new Bundle();bundle.putString("key", "我是主人,activity");fragment.setArguments(bundle);transaction.add(R.id.rightLayout, fragment);transaction.commit();}public void getRight(View v){//获取从Fragment中传来的值String values = fragment.getArguments().getString("data");Toast.makeText(MainActivity.this, "fragment传来的:"+values, 0).show();}}右边的Fragment:
package org.mobiletrain.fragment_demo07;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.Toast;public class RightFragment extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView view = inflater.inflate(R.layout.right, null);//向MainActivity传值getArguments().putString("data", "您好,我是fragment");Button rightButton = (Button)view.findViewById(R.id.rightBn);rightButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//获取从MainActivity传来的值String content = getArguments().getString("key");Toast.makeText(getActivity(),"activity说的:"+content, 0).show();}});return view;}}
0 0
- 通过bundle对象实现Fragment之间的传值
- Activity与Activity之间,Fragment与Fragment之间通过Bundle传值的研究
- Activity与Activity之间,Fragment与Fragment之间通过Bundle传值的研究
- android中传递复杂参数,activity之间和fragment之间的bundle传递集合/对象
- android中传递复杂参数,activity之间和fragment之间的bundle传递集合/对象
- android中传递复杂参数,activity之间和fragment之间的bundle传递集合/对象
- 不同Activity之间的数据传递---Bundle对象的实现
- Fragment之间Bundle传值
- Fragment之间使用Bundle传递对象,数据,序列化,集合
- <Bundle>通过bundle进行Activity之间的数据传递
- 使用回调函数实现Fragment之间的传值
- 使用Application实现Fragment之间的传值
- Fragment 之间的传值
- Ex3.10 不同Activity之间的数据传递——Bundle对象的实现
- bundle实现Activity之间的数据传递
- 不同Activity之间传递数据--Bundle对象和startActivityForResult方法的实现
- Android Fragment之间参数传递通过使用bundle进行参数传递
- Fragment之实现Fragment之间的通信
- HDU 4912 Paths on the tree(LCA+贪心)
- OpenCV 学习(几种基本的低通滤波)
- CF479 Riding in a Lift
- SaltStack学习笔记
- HDU - 3732 Ahui Writes Word(多重背包)
- 通过bundle对象实现Fragment之间的传值
- eclipse struts2 错误及解决方法
- lintcode-线段树的构造-201
- Android综合小练习Fragment,解析,Handler,ViewPager
- popupWindow的用法(1)
- UVA 1629 - Cake slicing 题解
- gym Learning to Add 一个水题 结果看错题意了
- hdu1426搜索
- 定义类class