Fragment向Fragment传值(第二种)
2015-07-28 23:10
387 查看
通过fragment指定的id 找到Fragment对象
代码
Fragment1:
Fragment2:
代码
Fragment1:
package com.qianfeng.fragmenttofragment2; import android.annotation.SuppressLint; 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 Fragment1 extends Fragment{ private Button btn1; private EditText et1; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_layout1, container, false); btn1 = (Button) view.findViewById(R.id.btn1); et1 =(EditText) view.findViewById(R.id.et1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String strValues = et1.getText().toString().trim(); //通过fragment指定的id 找到Fragment对象 Fragment2 fragment2 =(Fragment2) getFragmentManager().findFragmentById(R.id.fg2); fragment2.putValues(strValues); } }); return view; } }
Fragment2:
package com.qianfeng.fragmenttofragment2; 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{ private TextView tv_show; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_layout2, container, false); tv_show = (TextView) view.findViewById(R.id.tv_show); return view; } //这个方法是 对tv_show 进行赋值 public void putValues(String strValues){ tv_show.setText(strValues); } }
相关文章推荐
- magento 安装 ( 环境搭建)
- 《游园不值》——叶绍翁
- Java集合类图
- 【线段树】 HDU 5316 Magician
- android shape的使用
- Single Number II
- 一些windows下调试网络的命令行
- 【西祠日志】【10】周二周二
- 简单观察者模式
- 一些windows下调试网络的命令行
- C# chart
- HDU 4175 多时段多课最小能量消耗问题-dp-(数塔模型)
- clojure-基本语法-函数定义
- 把握linux内核设计思想(一):系统调用
- Hibernate基础概念
- BCB动态生成(用代码生成)按钮的方法
- R语言基础介绍
- 【linux高级程序设计】(第十章)Linux异步信号处理机制 3
- POJ 1273 最大流入门题 Edmonds_Karp算法
- Vmware虚拟网络详解