Fragment向Fragment传值(第一种)
来源:互联网 发布:云计算峰会的会议定位 编辑:程序博客网 时间:2024/05/21 04:19
1.fragment通过回调接口 将值返给Activity
2.Activity 通过setArguments 传值给另一个Fragment
代码:
Fragment1 :
package com.qianfeng.fragmenttofragment1;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;@SuppressLint("NewApi")public class Fragmet1 extends Fragment{private EditText et1;private Button btn1;CallBackValues callBackValues;@Overridepublic void onAttach(Activity activity) {// TODO Auto-generated method stubsuper.onAttach(activity);//得到activity中回调接口的实例化对象callBackValues =(CallBackValues) getActivity();}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stub/** * 第一个参数:布局资源id 唯一标示 * 第二个参数:表示当前父布局容器 * 第三个参数:表示是否追加父布局容器 */View view = inflater.inflate(R.layout.fragment_layout1, container, false);et1 = (EditText) view.findViewById(R.id.et1);btn1 = (Button) view.findViewById(R.id.btn1);//监听按钮点击事件 将值传给activitybtn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString strValues = et1.getText().toString().trim();callBackValues.SendMessage(strValues);}});return view;}//定义回调接口public interface CallBackValues{public void SendMessage(String StrValues);}}
Activity:
package com.qianfeng.fragmenttofragment1;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Fragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import com.qianfeng.fragmenttofragment1.Fragmet1.CallBackValues;@SuppressLint("NewApi")public class MainActivity extends Activity implements CallBackValues{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic void SendMessage(String StrValues) {// TODO Auto-generated method stub//得到FragmentManager对象FragmentManager manager = getFragmentManager();//得到FragmentTransaction对象FragmentTransaction transaction = manager.beginTransaction();//得到Fragment2 对象Fragment2 fragment2 = new Fragment2();Bundle bundle = new Bundle();bundle.putString("str", StrValues);fragment2.setArguments(bundle);//将fragment2 替换布局transaction.replace(R.id.contents, fragment2);transaction.commit();//提交事务}}
Fragment2:
package com.qianfeng.fragmenttofragment1;import android.annotation.SuppressLint;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;@SuppressLint("NewApi")public class Fragment2 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {/** * 第一个参数:布局资源id 唯一标示 第二个参数:表示当前父布局容器 第三个参数:表示是否追加父布局容器 */View view = inflater.inflate(R.layout.fragment_layout2, container,false);TextView tv_show =(TextView) view.findViewById(R.id.tv_show);//得到activity传过来的bundle对象Bundle bundle = getArguments();if(bundle!=null){tv_show.setText(bundle.getString("str"));}return view;}}
0 0
- Fragment向Fragment传值(第一种)
- Fragment向Fragment传值(第二种)
- Fragment向Fragment传值(第三种)
- activity向Fragment传值
- Activity向Fragment传值
- Activity向fragment传值
- Fragment向Activity传值
- Fragment向Activity传值
- Activity向Fragment传值
- Activity向Fragment传值
- 试水Fragment(1)-第一种声明方式
- Fragment向另一个Activity传值
- 45 Android activity 向 fragment 传值
- 安卓中实现Activity向Fragment传值
- 安卓中回调实现Fragment向Activity传值
- activity向另一个activity中的fragment传值
- 利用Bundle完成Activity向Fragment传值
- 向Fragment传递参数
- CSS总结(1)
- 解决Android中LayoutParam宽高的单位设置问题
- Android API Guides 阅读笔记(6)----Task and Back Stack
- VS2012启用SQLite的Data Provider
- leetCode(52):Add Binary
- Fragment向Fragment传值(第一种)
- linux常用命令----文本的统计
- HDU 4175 多时段多课最小能量消耗问题-dp-(数塔模型)
- UVA133-The Dole Queue
- Mac下eclipse安装SVN插件
- 我的关与<div>等块级元素浮动的总结
- Android中ArrayAdapter、SimpleAdapter、BaseAdapter总结
- c++成员指针笔记
- 一些windows下调试网络的命令行